Reputation: 23
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?
server:
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());
});
});
client:
<!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>
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"]}
Upvotes: 2
Views: 4841
Reputation: 180987
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());
});
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.
Upvotes: 3