我的服务器上的NodeJS和SocketIO设置有问题, 我想通过
访问HTTP端口var socket = io.connect(“http://domain.com:8070”); 遗憾的是,这会返回ERR_CONNECTION_TIMED_OUT
这是我的服务器:
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app, { log: false })
, fs = require('fs');
var mysocket = 0;
var socket = 0;
app.listen(8070);
function handler (req, res) {
fs.readFile(__dirname + '/app/tpl/skins/habbo/client.php',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
io.sockets.on('connection', function (socket) {
console.log('Client successfully connected');
mysocket = socket;
});
//udp server on 41181
var dgram = require("dgram");
var server = dgram.createSocket("udp4");
server.on("message", function (msg, rinfo) {
console.log("Packet recieved from server: " + msg);
if (mysocket != 0) {
mysocket.emit('field', "" + msg);
mysocket.broadcast.emit('field', "" + msg);
}
});
server.on("listening", function () {
var address = server.address();
console.log("udp server listening " + address.address + ":" + address.port);
});
server.bind(41181);
和我的客户:
<script src="http://domain.com:8070/socket.io/socket.io.js"></script>
<script type="text/javascript">
var socket = io.connect(http://domain.com:8070);
socket.on("hello", function(l){
var k = l.split(/,/);
switch(k){
case "testing":
{
window.alert('lol');
return;
}
}
});
</script>
关于如何让它在http://domain.com:8070上运行的任何想法?感谢。
答案 0 :(得分:0)
看起来您的套接字没有侦听端口8070。
未经测试,但值得一试:
var io = require('socket.io').listen(app.listen(8070), {log: false});
您也可以从客户端删除该网址(虽然它不会改变任何内容):
var socket = io.connect();
编辑:
您还应该将地址包装在引号中:
var socket = io.connect("http://domain.com:8070");