node.js + socket.io - 重复的 websocket 写入? [英] node.js + socket.io - duplicate websocket writes?

查看:38
本文介绍了node.js + socket.io - 重复的 websocket 写入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 node.js 和 socket.io 场景的新手,所以这可能是一个简单的修复......但这里是.我有以下 POC 用于通过 UDP 接收消息并将它们回显到浏览器.在 55555 上接收到 UDP 消息,执行 websocket 写入,并且客户端在其浏览器中看到数据.但是,当另一个客户端连接到服务器时,在接收到数据报时会执行四次 websocket 写入 - 我相信每个客户端两次,因为附加的数据现在显示两次.启动第三个客户端显示 6 次 websocket 写入 - 现在附加的数据出现了 3 次.

I'm new to the node.js and socket.io scene, so this may be a simple fix... but here goes. I have the following POC for taking in messages via UDP and echoing them back to a browser. UDP messages are received on 55555, a websocket write is performed, and the client sees the data in their browser. However, the moment another client connects to the server, four websocket writes are performed when a datagram is received - two to each client I believe, as the appended data now shows up twice. Firing up a third client shows six websocket writes - appended data appears three times now.

任何想法是什么导致了这种情况?愚蠢的错误?误解?错误?

Any ideas what is causing this? Dumb error? Misinterpretation? Bug?

服务器:

var dgram = require("dgram");
var udpserver = dgram.createSocket("udp4");
udpserver.bind(55555);  //udp listener

var express = require("express");
var app = express();
var http = require('http');
var server = http.createServer(app);
var io = require('socket.io').listen(server);
server.listen(12345);  //tcp http listener

io.sockets.on('connection', function (socket) {
    udpserver.on('message', function (msg, rinfo) {
            io.sockets.emit('update', msg.toString());
    });
});

客户:

<!DOCTYPE HTML>
<script src="http://code.jquery.com/jquery-1.8.1.min.js"></script>
<script src="http://localhost:12345/socket.io/socket.io.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        var socket = new io.connect('http://localhost:12345');
        socket.on('update', function (data) {
            $("#stuff").append(data + "<br>");
        });
     });
</script>
<div id=stuff></div>
</html>

以下是一些 node 输出,显示了第一个客户端连接后和第二个客户端连接后发送的数据:

Here's some node output showing data sent after the first client connects, and after a second one connects:

info  - socket.io started
debug - served static content /socket.io.js
debug - client authorized
info  - handshake authorized xrMZtzv9HPSngRYzMibw
debug - setting request GET /socket.io/1/websocket/xrMZtzv9HPSngRYzMibw
debug - set heartbeat interval for client xrMZtzv9HPSngRYzMibw
debug - client authorized for
debug - websocket writing 1::
debug - websocket writing 5:::{"name":"update","args":["test1\n"]}
debug - emitting heartbeat for client xrMZtzv9HPSngRYzMibw
debug - websocket writing 2::
debug - set heartbeat timeout for client xrMZtzv9HPSngRYzMibw
debug - got heartbeat packet
debug - cleared heartbeat timeout for client xrMZtzv9HPSngRYzMibw
debug - set heartbeat interval for client xrMZtzv9HPSngRYzMibw
debug - served static content /socket.io.js
debug - client authorized
info  - handshake authorized VJKxI4y8TV9tcYjEMibx
debug - setting request GET /socket.io/1/websocket/VJKxI4y8TV9tcYjEMibx
debug - set heartbeat interval for client VJKxI4y8TV9tcYjEMibx
debug - client authorized for
debug - websocket writing 1::
debug - websocket writing 5:::{"name":"update","args":["test1\n"]}
debug - websocket writing 5:::{"name":"update","args":["test1\n"]}
debug - websocket writing 5:::{"name":"update","args":["test1\n"]}
debug - websocket writing 5:::{"name":"update","args":["test1\n"]}

推荐答案

据我所知,没有节点可以测试;只要客户端不发送您需要处理的任何数据,您就不会真正对 socket.io 连接事件做任何事情,因此您应该能够忽略它们.你想要的只是接收UDP事件并广播给所有连接的客户端,这应该很简单;

As far as I can tell without having node to test on; you're not really interested in doing anything on socket.io connection events as long as the client doesn't send any data you need to handle, so you should be able to ignore them. What you want is only to receive UDP events and broadcast to all clients that are connected right then, which should be as simple as;

...
var io = require('socket.io').listen(server);
server.listen(12345);  //tcp http listener

udpserver.on('message', function (msg, rinfo) {
    io.sockets.emit('update', msg.toString());
});

只是为了完整性;您当前的代码所做的是为每个新的 socket.io 连接添加一个新的 UDP 消息回调.回调广播到所有客户端,这意味着当第二个客户端连接时,所有广播将发生2次给所有客户端,3个客户端3次给所有客户端等等.你只需要设置UDP消息处理程序一次,因为它无论如何都会向所有连接的客户端广播.

Just for completeness; what your current code does is to add a new UDP message callback for every new socket.io connection. The callback broadcasts to all clients, which means that when the second client connects, all broadcasts will happen 2 times to all clients, for 3 clients 3 times to all clients etc. You only want to set the UDP message handler up once since it broadcasts to all connected clients anyway.

这篇关于node.js + socket.io - 重复的 websocket 写入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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