好的,我有以下设置:https://i.stack.imgur.com/4T9SX.jpg(如果下面的图片太小)
问题是计算机2无法与socket.io连接到计算机1。
是的,我在计算机2中包含了socket.io:
为什么计算机2无法连接到带有socket.io的计算机1,而ping可以吗?
额外信息: - Socket.io版本1.4.5 - 两台电脑都是窗户10 - 计算机2 javascript在Phonegap中 - 计算机2通过Wi-Fi连接,计算机1通过以太网连接
问候
修改 来自客户端的代码(计算机2,启动时调用init):
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();
}));
}
服务器代码(计算机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);
});
});
浏览器(计算机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;
});
我想知道为什么计算机2甚至无法连接到服务器, console.log(“连接到套接字!”);甚至没有被称为
注意:如果我在计算机1上执行客户端(计算机2)的javascript,它可以正常工作,建立连接并接收数据!
注意:我使用计算机1(服务器)对其防火墙进行了测试,关闭并完美运行!
答案 0 :(得分:2)
请检查防火墙设置,或将其关闭几分钟而不是尝试,同时确保两台计算机都应使用相同的局域网/ wifi连接。和
通过Windows防火墙允许 phonegap,Evented I / O v8 JavaScript和安全套接字隧道协议,
答案 1 :(得分:1)
在图像中引用代码时,您需要先检查以下内容
如果您遇到问题,可以参考以下代码启动服务器
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);
});
这应该适合你。