62

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.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
sw.
  • 3,240
  • 2
  • 33
  • 43
  • Researching further I can see that the bug is located around the socket.io code because if I test the same code using the net library under nodejs with two listening servers everything works fine. Currently I don't have the knowledge to pinpoint if there is a bug in my code, in socket.io or in nodeJS. – sw. Aug 25 '11 at 20:04
  • More information: it seems related to telnet control chars and/or "\n" – sw. Aug 26 '11 at 05:03
  • Hmm, I can't seem to replicate this problem. What do you mean by string received from the console scope "*If I send a string received from the console scope [...]*"? – WTK Sep 13 '11 at 11:58
  • What socket.io version are you using? – sw. Sep 16 '11 at 18:36
  • The latest that I've found on the github. – WTK Sep 17 '11 at 06:51
  • Is there any reason for not updating the socket.io to the latest version? 0.8.4 right now. Maybe update would solve your problem. – WTK Sep 18 '11 at 16:32
  • The post was before the 0.8 release. I'll retest it in the coming weeks. – sw. Sep 21 '11 at 01:57
  • Socket.IO grades it's connection based on what the client can do, and tries to cache that grade for future use. – EhevuTov Oct 08 '11 at 01:11
  • Are the local and remote machines the same platform or is one Windows and the other Unix/Linux? – Paul Jackson Oct 20 '11 at 12:07
  • can you post what the log files are showing? Also - try 'popo\n' or 'popo\r'. Further - try removing the last char or two from the inbound string to see if that helps. You could also try URLencoding the message before you send it and then decoding it on the server. – ethrbunny Oct 20 '11 at 12:38
  • @PaulJackson they are in the same machine: Linux. – sw. Nov 14 '11 at 02:17

2 Answers2

8

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

Michael Durrant
  • 93,410
  • 97
  • 333
  • 497
-3

This is just a case where you need to dynamically allocate memory locally. When the string comes in you should do a string copy of the string into a dynamically allocated string of the same length. problem solved.

Androider
  • 21,125
  • 36
  • 99
  • 158