socket.emit 在一个用 NodeJS 编写的简单 TCP 服务器中? [英] socket.emit in a simple TCP Server written in NodeJS?

查看:47
本文介绍了socket.emit 在一个用 NodeJS 编写的简单 TCP 服务器中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[如你所见,我不太了解 TCP 服务器和客户端的基本概念,可能 socket.emit 甚至不可能,但我想知道最好的替代方案或类似的东西...]

[as you'll see, I don't understand the basic concepts of a TCP server and client very well and probably socket.emit is not even possible, but I'd like to know the best alternative or similar thing...]

Socket.io 有一个美妙的东西来发射事件并在另一端捕捉它们,它在它的第一页 (http://socket.io).我可以用 NodeJS 的常规net"模块做类似的事情吗?如果不是,那么等价物是什么?

Socket.io has a beautiful thing to emit events and catch them on the other side, it's in it's first page (http://socket.io). Can I do something similar like that but with NodeJS' regular 'net' module ? If not then what's the equivalent ?

我试过了:

server.js

var server = net.createServer(function(socket) {
    socket.on("connect",function() {
        socket.emit('test',{msg : 'did you get it ?'});
    });
}).listen(8000);

client.js

var client = net.createConnection(8000, localhost);
client.on('connect',function(){
    client.on('test',function(data) {
        console.log(data.toString());
    });
});

但正如你想象的那样,它不起作用.我怎样才能做到这一点?

But as you can imagine, it does not work. How can I achieve this ?

提前致谢.

推荐答案

好吧,net 只是 TCP 的一个接口.要发送和接收消息,您需要在 TCP 之上设计和实现您自己的协议.TCP 是面向流的协议,而不是面向消息的协议.这意味着您必须发明一种方法让读者分开消息.分隔消息的最简单方法是在它们之间插入 \n 字符.将消息编码为字节流的最简单方法是使用 JSON.stringify.所以:

Well, net is just an interface to TCP. To send and receive messages you need to design and implement your own protocol on top of TCP. TCP is a stream-oriented protocol and not message-oriented. This means that you must invent a way for the reader to separate messages. The simplest way to separate messages are to insert \n characters between them. The simplest way to encode messages as a byte stream is to use JSON.stringify. So:

client.js

var Lazy = require('lazy'), net = require('net')

var client = net.createConnection(8000)

new Lazy(client).lines.forEach(function (msg)
{
    console.log(JSON.parse(msg))    
})

server.js

var net = require('net')

var server = net.createServer(function(socket) {
    socket.on("connect",function() {
    var str = JSON.stringify({foo : 'test', msg : 'did you get it ?'}) + "\n"
        socket.write(str)
    });
}).listen(8000);

您可以从这里开始并详细说明.例如,您可以在接收器端使用 EventEmitter 库类,并在接收到不同的消息时发出不同的事件.

You can start from this and elaborate. For example, you can use EventEmitter library class on receiver side and emit different events upon receiving different messages.

'lazy' 模块在 NPM 上可用,用于将接收字节流拆分为单独的行.拆分是可以手动完成的,但它需要 20 多行代码.有关拆分的示例实现,请参阅脏"NPM 模块的来源 - 它很麻烦,因此在这种情况下具有外部依赖性是有充分根据的.

The 'lazy' module is available on NPM and is used to split the receiving byte stream into separate lines. The splitting is doable by hand, but it will require like 20 more lines of code. See the sources of 'dirty' NPM module for en example implementation of splitting - it's cumbersome, so having an external dependency is well-grounded in this case.

这篇关于socket.emit 在一个用 NodeJS 编写的简单 TCP 服务器中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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