Phaser.js中的Node.js和Socket.io没有连接socket.io/?EIO=3&transport=polling

时间:2017-01-25 16:22:17

标签: javascript node.js express socket.io phaser-framework

我可以运行我的node.js服务器,我的phaser.js游戏运行但我没有连接'游戏运行时的console.log。我反而从客户端收到此错误消息:

enter image description here

socket.io-1.4.5.js:1 GET http://192.168.128.184:8080/socket.io/?EIO=3&transport=polling&t=LdMR6Ro net :: ERR_CONNECTION_REFUSED

SERVER:

var serverPort = 8080;

console.log("Initializing Server.");

var express = require('express');
var connect = require('connect');
var app = express();
var serv = require('http').Server(app);  //.createServer(app);
var io = require('socket.io').listen(serv);  //(serv,{});

console.log("Starting Server.");

var serveStatic = require('serve-static');
connect().use(serveStatic(__dirname)).listen(serverPort, function(){
    console.log('Server running on ' + serverPort + ' !');
});

app.get('/',function(req, res) {
    res.sendFile(__dirname + '/index.html');
});

serv.listen(8081);

var SOCKET_LIST = {};
io.on('connection',function(socket){  
    console.log("A user is connected");
});

io.sockets.on('connection', function(socket){
    console.log('Socket connection');
});

客户端

var local = "http://" + document.location.host + ":8081";
        var socket = io().connect(local);

1 个答案:

答案 0 :(得分:2)

在您的客户端代码中,您使用的是io().connect(local),但使用指定地址与变量进行连接的正确方法是io.connect(local)

此外,document.location.host将包含":8080"如果它是您用于获取页面的地址的一部分,则需要将其删除。您可以尝试document.location.host.split(':')[0]

之类的内容