Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket.IO issue with control chars

I am implementing an application that uses websockets and a console accessed via Telnet. There is a communication between the connection established via websockets and the console. I am experiencing a weird issue:

  • If I send a string constant to an established socket when something is entered in the console it works ok.
  • If I send a string received from the console scope It seems to open a new socket (not sure) because in the debug log I see it and in the browser side (websockets) it alerts me of a new connection.
  • If I send a local string (instead of the one received from the other scope) it's sent correctly. (commented line: client.send(message) )

I share here the nodeJS code, take into account that this is now a test app so it's assumed only one socket and websockets connection:

// Sample based on: http://elegantcode.com/2011/05/04/taking-baby-steps-with-node-js-websockets/ // Changed for sockets.io 6.x => 7.x   var events = require('events'); var eventEmitter = new events.EventEmitter();  var http = require('http'); var socketIO = require('socket.io'); var static = require('node-static');  var port = 2000;  var clientFiles = new static.Server('./client');  var httpServer = http.createServer(     function(request, response) {         request.addListener('end', function() {              clientFiles.serve(request, response);        });     })  httpServer.listen(port); console.log("Server running at port: " + port);  var io = require('socket.io').listen(httpServer);  var webSocket = io.sockets;  webSocket.on('connection', function(client) {     client.send('Please enter a user name ...');     var userName;      eventEmitter.on('someOccurence', function(message) {         console.log("Received: " + message);         client.send('Line Received');         var s = 'popo';         client.send(s);         //client.send(message);     });       client.on('message', function(message) {         console.log(message);          if(!userName) {             userName = message;             client.broadcast.send(message + ' has entered the zone.');             return;         }          var broadcastMessage = userName + ': ' + message;         client.broadcast.send(broadcastMessage);         client.send("Repeated here: " + message);     });      client.on('disconnect', function() {         var broadcastMessage = userName + ' has left the zone.';         client.broadcast.send(broadcastMessage);     }); });   var lines = require('lines-adapter'); var net = require('net');  var server = net.createServer(function (socket) {     socket.write("Welcome to the Server\r\n");      lines(socket, 'utf8')     .on("data",         function(line) {             console.log("Line received: " + line);             eventEmitter.emit('someOccurence', line);         }     )     .end("end",         function() {             console.log("End of Stream");         }     ); });  server.listen(1337, "127.0.0.1"); 

UPDATE: I just tested with socket.io 0.8.6 and nodeJS 0.4.12 without this issue. I will keep this question here for future reference.

like image 690
sw. Avatar asked Aug 25 '11 11:08

sw.


People also ask

Is Socket.IO better than WebSocket?

Socket.IO is way more than just a layer above WebSockets, it has different semantics (marks messages with name), and does failovers to different protocols, as well has heartbeating mechanism. More to that attaches ID's to clients on server side, and more. So it is not just a wrapper, it is full-featured library.

Does Socket.IO use long-polling?

The bidirectional channel between the Socket.IO server (Node. js) and the Socket.IO client (browser, Node. js, or another programming language) is established with a WebSocket connection whenever possible, and will use HTTP long-polling as fallback.

Is Socket.IO difficult?

All in all, the Socket.io code was incredibly simple.

Is Socket.IO scalable?

Scalability problems with Socket.IOjs (and therefore Socket.IO) is single threaded, meaning that it cannot take advantage of multi-core processors. In order to do this, we need to run a separate Node. js instance for each core. However, these instances would be completely independent and not share any data.


1 Answers

Fixed with socket.io v0.8.6. This q should be answered/closed.

like image 111
Michael Durrant Avatar answered Sep 25 '22 23:09

Michael Durrant