4

Okay, I have the following setup: https://i.stack.imgur.com/4T9SX.jpg ( If image below is too small )

enter image description here

The problem is that Computer 2 can not connect with socket.io to computer 1.

enter image description here

Yes i included socket.io in computer 2:

enter image description here

Any ideas as to why Computer 2 cannot connect to computer 1 with socket.io whilst ping can?

Extra information: - Socket.io version 1.4.5 - Both computers are windows 10 - Computer 2 javascript is in Phonegap - Computer 2 connects via wi-fi, computer 1 via ethernet

Greetings

EDIT Code from client (computer 2, init is called upon start):

KerstAppHome.prototype.init = function(){
    var address = 'http://192.168.2.120:2017';
    console.log("Connecting to: " + address);
    this.socket = io.connect(address);
    this.socket.on('connect', this.proxy(function(){
        console.log("Connected to socket!");
        this.socketIsConnected = true;

        this.socket.on('disconnect', this.proxy(function(){
            console.log("Disconnected from socket!")
            this.socketIsConnected = false;
        }));
        this.socket.on('musicBlob', this.proxy(this.onMusicBlobReceived))
    }));

};

KerstAppHome.prototype.onMusicBlobReceived = function(musicBlob){
    console.log("RECEIVED SOMETHING");
    this.audioCtx.decodeAudioData(musicBlob).then(this.proxy(function(audioBuffer) {
        var source = this.audioCtx.createBufferSource();
        source.buffer = audioBuffer;
        source.connect(this.audioCtx.destination);
        source.start();
    }));
}

Code from server (computer 1):

var port = 2017;
var io = require('socket.io')(port);
console.log("Listening for socket connections on port " + port);

io.on('connection', function (socket) {
    console.log("Connection made!");
    socket.on('musicBlob', function(musicBlob){
        socket.broadcast.emit('musicBlob', musicBlob);
    });
});

Relevant code from browser ( computer 1 ):

var socket = io.connect('http://localhost:2017');
var socketIsConnected = false;

socket.on('connect', function(){
    console.log("Connected to server!");
    socketIsConnected = true;
});
socket.on('disconnect', function(){
    console.log("Disconnected from server!")
    $scope.socketIsConnected = false;
});

I want to know why computer 2 can't even connect to the server, The console.log("Connected to socket!"); is not even called

NOTE: If I execute the javascript of the client (computer 2) on computer 1, it works perfectly, makes connection and receives data!

NOTE: I tested it with computer 1 (server) his firewall turned off and it worked perfectly!

ErikBrandsma
  • 1,345
  • 2
  • 15
  • 42
  • We need to see the actual code for both computers (pasted as text, not images) into this question. – jfriend00 Nov 13 '16 at 16:13
  • You are using a custom path with `io.connect()` in the client, but you have not specified that you are listening for that custom path in your server. Either remove the custom path from the client (I see no reason for it) or add the custom path to your server configuration as shown here: http://stackoverflow.com/questions/29511404/connect-to-socket-io-server-with-specific-path-and-namespace – jfriend00 Nov 13 '16 at 16:16
  • Can you try a different port? Is it maybe blocked by firewall settings? – user835611 Nov 13 '16 at 16:49
  • @user835611 I have made 2 rules in windows firewall, one for TCP and one for UDP that allow any connection over port 2017, i have also tried port 8080 but with no success, thank you for the suggestion though! – ErikBrandsma Nov 13 '16 at 17:15
  • @jfriend00 The reason a custom path is in the client computer 2 is that "localhost" would not work as "localhost" would point to computer 2 his IP and not computer 1 his IP. I will try your stackoverflow post! – ErikBrandsma Nov 13 '16 at 17:17
  • The "path" I refer to is the `/audio` you show on the URL you are trying to connect to. You can't just make up a path like that on the client end. – jfriend00 Nov 13 '16 at 17:21
  • The rules here say that the relevant code should be actually pasted into the question and formatted properly, not only available via an external link. That's because external links tend to change or disappear over time rendering the question useless as a long term reference. External links are OK for additional context, but the exact code relevant to the question should be pasted directly into the question. – jfriend00 Nov 13 '16 at 17:22
  • @jfriend00 I understand, relevant code pasted in the editor! I noticed that when I run the client code (computer 2) in computer 1, it works perfectly and the client receives data! How can this be? – ErikBrandsma Nov 13 '16 at 19:58
  • This might be because your firewall is blocking connections on this port. If you just try to display a normal page on computer 2 (the one with the server), without socket.io, does the HTML display correctly? – nicovank Nov 16 '16 at 21:02
  • The HTML page loads correctly and I have made 8 inbound / outbound rules for both client and server for port 2017 in both UDP and TCP, still nothing.. I thought that after your comment it was the missing outbound rule in the server computer (computer 1), but unfortunately it was not :( – ErikBrandsma Nov 16 '16 at 21:40
  • 1
    Can you post the output of `netstat -ant` from both the computers when the server is running? It could be that the socket.io server is listening on a different interface than the one that is facing to the network. The netstat output will give more details. – Surojit Nov 17 '16 at 08:50
  • please check you firewall settings, or turn it off for few minutes and than try, also make sure both 2 computer should be connected with same lan/wifi. – Vinay Pandya Nov 18 '16 at 10:32
  • @VinayPandya I tested this with computer 1 his firewall turned off (this computer has the socket.io server) AND IT WORKED! :D Now I don't want to turn off my entire firewall for this to work, any idea's what part of the firewall is blocking this and how to fix this? – ErikBrandsma Nov 19 '16 at 13:31
  • @nicovank turned off firewall on computer 1 (server computer) and it worked! Any idea why? And how to fix this without turning off the entire firewall? – ErikBrandsma Nov 19 '16 at 13:32
  • 1
    @user835611 It was indeed the firewall! Turned it off on computer 1 for a test and it worked perfectly, now I don't want to turn off my entire firewall, any idea's on how to fix this? – ErikBrandsma Nov 19 '16 at 13:32
  • @ErikBrandsma i am very glad that my answer helped you.on local machine u have to turn off firewall, or you can allow node js to allow communicate through the firewall. on server/in production u will not face this kind of problem. if my answer works for u than let me know i will post my answer and plz mark my answer. – Vinay Pandya Nov 19 '16 at 15:01
  • @ErikBrandsma i have added answer, plz mark my answer. – Vinay Pandya Nov 21 '16 at 05:12
  • @ErikBrandsma plz mark the answer. – Vinay Pandya Nov 22 '16 at 05:05

2 Answers2

2

please check you firewall settings, or turn it off for few minutes and than try, also make sure both 2 computer should be connected with same lan/wifi. and

allow phonegap, Evented I/O for v8 JavaScript and Secure Socket Tunneling Protocol through Windows Firewall,

Vinay Pandya
  • 2,701
  • 1
  • 23
  • 39
1

As referencing your code in image - you need to check the following things first

  1. What is the exposed url for your nodejs like e.g.localhost:2017 - if you have set it up as locahost - your node server is running as uri of hostname of localhost and ip 127.0.0.1 which cannot be accessed from another machine - if this is the case you need to assign the actual ip to node process 192.168.2.120 (in your case) only then you can access it from another machine
  2. its good to have a namespace to socket,io connect process
  3. are you using windows machine? you need to open network and sharing center - if you are connected to public network - connections will never work - at least the machine running nodejs server should be connected to home or office network

if you are stll facing the issue you can refer the following code for starting the server

const http = require('http');
const url = '192.168.2.120';
var port = 2017;

console.log("Listening for socket connections on port " + port);

var requestListener = function (req, res) {
     res.writeHead(200);
     res.end('Hello, World!\n');
}

var server = http.createServer(requestListener);
var io = require('socket.io')(server);

server.listen(port,url);

console.log('Server running at:', url+':'+port);

io.on('connection', function (socket) {
    console.log("Connection made!");
    socket.on('musicBlob', function(musicBlob){
    socket.broadcast.emit('musicBlob', musicBlob);
});

This should work out for you.

Sohan Mahajan
  • 17
  • 1
  • 5