1

I have a personal userscript enabled on an HTTPS website. The userscript is using Socket.io client API to make a connection to a Node.js TCP server in my PC

The server notices that the client is connecting to the server, but the client doesn't notice the connection and instead receives this error from the browser console

Failed to load resource: net::ERR_TIMED_OUT

I don't know how to resolve this error, but I'm assuming it has something to due with Socket.io and Net being incompatible with each other or the website is not allowing the connection to happen

Here is my client code

socket.on('connect', function() {
    console.log('CONNECTED TO SERVER');
    socket.on('message', function(msg) {
        messageReceive(msg);
    });
    setInterval(runRoutine, 1000);
});

My sever code

var net = require('net');

var clients = [];

var port = 8841;

net.createServer(function(socket) {

    socket.name = socket.remoteAddress + ':' + socket.remotePort;
    clients.push(socket);

    broadcast(socket.name + " has connected to the server", socket);

    socket.on('message', function(data) {
        broadcast(socket.name + "<>" + data, socket);
    });

    socket.on('end', function() {
        clients.splice(clients.indexOf(socket), 1);
        broadcast(socket.name + '<>disconnected', socket);
    });

    function broadcast(message, sender) {
        console.log(message);
        clients.forEach(function(client) {
            if(client === sender) return;
            //client.write(message);
        });
    }

}).listen(port);

console.log("Chat server running at port " + port);
Streak324
  • 153
  • 1
  • 3
  • 12
  • You can't do this because `socket` here on the server side is a TCP socket, not a socket.io socket, so you won't get `'message'` events. Why are you using a TCP server anyway instead of a socket.io server? – mscdex Aug 30 '16 at 17:50

1 Answers1

0

Javascripts can't use tcp sockets directly from a browser, however websocket and http protocols are available. Soccket.io uses websockets if it is able to. So you server side should listen to websocket connections. See example here

Community
  • 1
  • 1
Dennis R
  • 1,769
  • 12
  • 13