我使用本机“net”库在nodejs中编写了一个简单的Socket Client。
每当我向服务器发送消息时,客户端都会收到一条带有自己消息的on('data')事件。
是否可以避免这种类型的环回? 我只关心来自套接字服务器的消息而不是我自己的消息。
var client = new net.Socket();
// 1. Connect
client.connect(10000, '127.0.0.1', function() {
console.log('Connection to net Server successful');
client.write('Hello, server! I just connected to you.'); // <--this...
});
client.on('data', function(data) {
console.log('Received for server: ' + data); // <--...will fire in here
});
以下是上述代码执行后的日志输出。
Connection to net Server successful
Received from server: Echo server
// ^ This came from the socket server.
Received from server: Hello, server! I just connected to you.
// ^ This is wrong, since this is what I sent to the socket server.