简体   繁体   中英

Socket.io doesn't work in production

I have tried socket.io on localhost and it works perfectly. However, when in production I get the following error:

GET http://77.235.46.164:3000/socket.io/?EIO=3&transport=polling&t=1448754369321-21 net::ERR_CONNECTION_TIMED_OUT

On the server side I have the following code:

var express = require('express');
var mysql = require('mysql');
app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server);
connection.connect();
server.listen(process.env.PORT || 3000);

While on the client side:

var socket = io.connect('http://77.235.46.164:3000'); .... etcs

I am also starting the node server (I have VPS account in EuroVPS). I have searched everywhere but I don't seem to find a solution that works for me.

You can create an HTTP server yourself,instead of having the Express framework create one for you, so you will be able to reuse your HTTP server, in order to keep the same server instance.

You can try :

var express = require('express');
var app = express();

//Create your server
var server = app.listen(3000);
var io = require('socket.io')(server);

//Then you can listen for connection
io.on('connection', function(socket){
    socket.emit('an event sent to all connected clients')
  //Awesome things
})

Is it working without firewall?

In some controlled network Firewall may block websockets & ports other than 80,443.

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