简体   繁体   中英

Node.js and Socket.io in Phaser.js not connecting socket.io/?EIO=3&transport=polling

I am able to run my node.js server, my phaser.js game runs but I get no 'connected' console.log when the game runs. I instead get this error message from the client end:

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');
});

CLIENT

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

In your client code you are using io().connect(local) however the correct way to connect using a specified address with your variable is io.connect(local) .

Also, document.location.host will include ":8080" if it is a part of the address you used to obtain the page, therefore you need to remove it. You can try something like document.location.host.split(':')[0]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM