问题
Okay, I have the following setup: https://i.stack.imgur.com/4T9SX.jpg ( If image below is too small )
The problem is that Computer 2 can not connect with socket.io to computer 1.
Yes i included socket.io in computer 2:
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!
回答1:
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,
回答2:
As referencing your code in image - you need to check the following things first
- 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
- its good to have a namespace to socket,io connect process
- 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.
来源:https://stackoverflow.com/questions/40575855/socket-io-local-network-not-connecting