Node.js中的同步TCP读取 [英] Synchronous TCP Read in Node.js

查看:170
本文介绍了Node.js中的同步TCP读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在node.js中同步读取TCP套接字?

Is there a way to do a synchronous read of a TCP socket in node.js?

我很清楚如何通过向套接字的"data"事件添加回调来异步执行此操作:

I'm well aware of how to do it asynchronously by adding a callback to the socket's 'data' event:

socket.on('data', function(data) {
    // now we have the string data to do whatever with
});

我也知道,尝试使用函数调用来阻止而不是注册回调与节点的设计背道而驰,但是我们正在尝试更新一个旧的节点模块,该模块充当我的大学的客户端,同时保持向后兼容性.所以我们目前有:

I'm also aware that trying to block with a function call instead of registering callbacks goes against node's design, but we are trying to update an old node module that acts as a client for my university while maintaining backwards compatibility. So we currently have:

var someData = ourModule.getData();

getData()之前有很多逻辑,但是现在我们只想发送到服务器"run getData()"并等待结果.这样,所有逻辑都在服务器端,而不是重复的客户端和服务器端.该模块已经维护了与服务器的TCP连接,因此我们只是在此基础上.

Where getData() previously had a bunch of logic behind it, but now we just want to send to the server "run getData()" and wait for the result. That way all logic is server side, and not duplicated client and server side. This module already maintains a TCP connection to the server so we are just piggybacking on that.

这是我尝试过的解决方案:

Here are the solutions I've tried:

  1. 在节点的net模块中为类似于python套接字库的隐藏套接字找到阻塞读取功能.

  1. Find a blocking read function for the socket hidden somewhere similar to python's socket library within node's net module.

string_from_tcp = socket.recv(1024)

这里的问题是它似乎不存在(不足为奇,因为它违背了节点的意识形态).

The problem here is that it doesn't seem to exist (unsurprisingly because it goes against node's ideology).

syncnet模块添加了我所需的内容,但不支持Windows;所以我必须添加它.

This syncnet module adds what I need, but has no Windows support; so I'd have to add that.

找到一个函数,该函数允许节点解除对事件循环的阻塞,然后返回,这样可以正常工作:

Find a function that allow's node to unblock the event loop, then return back, such that this works:

var theData = null;
clientSocket.on('data', function(data) {
    theData = data;
});

clientSocket.write("we want some data");

while(theData === null) {
    someNodeFunctionThatUnblocksEventLoopThenReturnsHere(); // in this function node can check the tcp socket and call the above 'data' callback, thus changing the value of theData
}

// now theData should be something!

一个明显的问题是我不认为存在这种东西.

Obvious problem here is that I don't think such a thing exists.

使用ECMAScript 6生成器功能:

Use ECMAScript 6 generator functions:

var stringFromTcp = yield socketRead(1024);

这里的问题是,我们将迫使学生将其JavaScript客户端更新为这种新语法,并且理解ES6不在使用此语法的课程范围之内.

The problem here is that we'd be forcing students to update their JavaScript clients to this new syntax and understanding ES6 is outside the scopes of the courses that use this.

使用node-gyp并向我们的节点模块中添加一个C ++ TCP库的接口,该接口确实支持同步读取,例如boost的asio.这可能会起作用,但是要使节点模块与Boost跨平台一起编译是一个巨大的痛苦.因此,我来​​参加Stack Overflow以确保我不会使这个问题变得过于复杂.

Use node-gyp and add to our node module an interface to a C++ TCP library that does support synchronous reads such as boost's asio. This would probably work but getting the node module to compile with boost cross platform has been a huge pain. So I've come to Stack Overflow to make sure I'm not over-complicating this problem.

最简单的说,我只是试图创建一个支持同步tcp读取的命令行JavaScript程序.

In the simplest terms I'm just trying to create a command line JavaScript program that supports synchronous tcp reads.

还有其他想法吗?如果在节点项目的上下文中这似乎是亵渎神明,请提前表示歉意,并感谢您的任何投入.

So any other ideas? And sorry in advance if this seems blasphemous in context of a node project, and thanks for any input.

推荐答案

我最终选择了选项5.我发现了一个小型,快速且易于构建的C ++ TCP库( netlinkwrapper .

I ended up going with option 5. I found a small, fast, and easy to build TCP library in C++ (netLink) and wrote a node module wrapper for it, aptly titled netlinkwrapper.

该模块可在Windows和Linux上构建,但是由于它是C ++插件,因此需要配置node-gyp才能构建它.

The module builds on Windows and Linux, but as it is a C++ addon you'll need node-gyp configured to build it.

我希望没有其他人像使用此模块那样需要使用Node.js,但是如果您必须使用TCP调用阻止事件循环,那么这可能是您唯一的选择.

I hope no one else has to screw with Node.js as I did using this module, but if you must block the event loop with TCP calls this is probably your only bet.

这篇关于Node.js中的同步TCP读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆