Duplicating data on node.js TCP socket

I'm connecting to node servers via a TCP socket and am having issues with the data being written to the socket duplicating on each subsequent call. My server call the following function that writes to each connected client: clients.forEach(function (client) { var string = "hello"; client.write(string); client.on('data', function(data) { console.log(data.toString()); }); }); This works and broadcasts the string "hello" to each of the connected sockets. The idea is that each client will reply with a response which will be output to console.log. On each client I have the following: client.on('data', function(data) { console.log(data); var response = "Hello back"; console.log('RES: ' + response); client.write(response); }); Each client receives hello and responds to the request. The issue arises that on each N response the server receives everything N times from each client. Therefore on the first exchange it is simply Hello Hello Back but on the second it is: Hello Hello Back Hello Back Any ideas why this could be happening? Do I need to flush the socket on each exchange or something?
Doesn't client.on('data',...) add a new callback every time you call it?

以上就是Duplicating data on node.js TCP socket的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏