Monday 8 May 2017

Why does my socket.io app disconnect immediately when run on Raspberry pi?

I am attempting to build a node.js application on a Raspberry Pi which can communicate with a remote socket.io server.

The socket.io server quite simply writes to the console when a new connection is established and when an existing connection is closed.

The socket.io client works as expected when run in a browser.

However, when I run the client code from a Raspberry Pi it connects and immediately terminates. This does not allow me to perform any function such as emitting or receiving.

The on connect event is never fired.

var io = require('/usr/local/lib/node_modules/socket.io-client');
var serverUrl = 'http://remoteserver.com';

console.log('Connecting to ' + serverUrl);
var socket = io.connect(serverUrl);

socket.on('connect', function(socket) {
    console.log('Connection established!');
    console.log(socket.id);

    socket.on('disconnect', function() {
            console.log('Disconnected from server');
    });

});

socket.close();
console.log('Connection closed.');

The code above will output:

Connecting to: http://remoteserver.com
Connection closed.

On the server side, the output will show a new connection and eventually a timeout close.

Why is the client on connect event not firing? How can I persist this connection so that I can eventually send inputs to the server?



via aaronfarr

No comments:

Post a Comment