我在这里研究了25篇以上的文章,关于我无法连接到运行在iMac(osx Mavericks)上的node.js网络服务器的可能原因。
我已经成功设置了我的测试项目,它在localhost(端口3000)上运行良好。我将index.html文件放在远程服务器(网站)上,但它不再连接到运行Node.js服务器的家用计算机。
我有:
- 仔细检查了一切
- 关闭防火墙
- 尝试将端口(3000)绑定到' 0.0.0.0'
- 从我的家庭wifi切换到我的移动蜂窝互联网(以确保路由器无法阻止我的端口)
- 尝试将侦听器端口更改为:8080,8888,80,3000等...
- 等等。
非常感谢帮助,否则我想我放弃......
如果我使用netstats,这就是我得到的:
tcp4 0 0 *.3000 *.* LISTEN
tcp4 0 0 *.17500 *.* LISTEN
tcp4 0 0 127.0.0.1.17603 *.* LISTEN
tcp4 0 0 127.0.0.1.17600 *.* LISTEN
tcp46 0 0 *.7348 *.* LISTEN
tcp46 0 0 *.7347 *.* LISTEN
tcp4 0 0 *.3234 *.* LISTEN
tcp4 0 0 192.168.1.5.3233 *.* LISTEN
tcp4 0 0 *.88 *.* LISTEN
tcp4 0 0 *.22 *.* LISTEN
tcp4 0 0 127.0.0.1.631 *.* LISTEN
tcp4 0 0 *.445 *.* LISTEN
tcp4 0 0 *.5900 *.* LISTEN
tcp4 0 0 *.548 *.* LISTEN
server.js:
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var clients =[];
app.use("/www", express.static(__dirname + '/public/'));
app.get('/', function(req, res){
res.sendFile(__dirname + '/whiteboard.html');
});
io.on('connection', function(socket){
console.info('New client connected (id=' + socket.id + ').');
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
index.html(网站)
<!doctype html>
<html>
<head>
<title>Socket.IO Webpage</title>
<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script type="text/javascript">
var url = 'http://109.166.x.x:3000';
var socket = io.connect(url);
socket.on('moving', function (data) {
if(! (data.id in clients)){
// a new user has come online.
}
});
</script>
</head>
<body>
</body>
</html>