简体   繁体   English

Node.js中的同步TCP读取

[英]Synchronous TCP Read in Node.js

Is there a way to do a synchronous read of a TCP socket in node.js? 有没有办法在node.js中同步读取TCP套接字?

I'm well aware of how to do it asynchronously by adding a callback to the socket's 'data' event: 我很清楚如何通过向套接字的'data'事件添加回调来异步执行它:

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();

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. 其中getData()之前有一堆逻辑,但现在我们只想发送到服务器“run getData()”并等待结果。 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. 该模块已经维护了与服务器的TCP连接,因此我们只是捎带它。

Here are the solutions I've tried: 以下是我尝试过的解决方案:

  1. Find a blocking read function for the socket hidden somewhere similar to python's socket library within node's net module. 找到隐藏在某个地方的套接字的阻塞读取函数,类似于节点网络模块中的python的套接字库。

     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). 这里的问题是它似乎不存在(不出所料,因为它违背了节点的意识形态)。

  2. This syncnet module adds what I need, but has no Windows support; syncnet模块添加了我需要的内容,但没有Windows支持; so I'd have to add that. 所以我必须补充一点。

  3. 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. 这里显而易见的问题是我不认为存在这样的事情。

  4. Use ECMAScript 6 generator functions: 使用ECMAScript 6生成器函数:

     var stringFromTcp = yield socketRead(1024); 

    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. 这里的问题是我们强迫学生将他们的JavaScript客户端更新为这种新语法,并且理解ES6不在使用它的课程范围内。

  5. 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. 使用node-gyp并向我们的节点模块添加一个接口,该接口指向支持同步读取的C ++ TCP库,例如boost的asio。 This would probably work but getting the node module to compile with boost cross platform has been a huge pain. 这可能会有效但是使用boost cross平台编译节点模块一直是一个巨大的痛苦。 So I've come to Stack Overflow to make sure I'm not over-complicating this problem. 所以我来Stack Overflow确保我不会过度复杂化这个问题。

In the simplest terms I'm just trying to create a command line JavaScript program that supports synchronous tcp reads. 用最简单的术语我只是想创建一个支持同步tcp读取的命令行JavaScript程序。

So any other ideas? 还有其他想法吗? And sorry in advance if this seems blasphemous in context of a node project, and thanks for any input. 如果在节点项目的上下文中这似乎是亵渎神明的话,请提前抱歉,并感谢任何输入。

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 . 我最终选择了5.我在C ++( netLink )中找到了一个小型,快速且易于构建的TCP库,并为其编写了一个节点模块包装器,名称为netlinkwrapper

The module builds on Windows and Linux, but as it is a C++ addon you'll need node-gyp configured to build it. 该模块建立在Windows和Linux之上,但由于它是一个C ++插件,因此您需要配置node-gyp来构建它。

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调用来阻止事件循环,这可能是你唯一的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM