Socket.io gives CORS error even if I allowed cors it on server

后端 未结 5 1941
名媛妹妹
名媛妹妹 2020-12-17 15:31

My node server and client are running on different ports(3001,5347 respectively). On client I had used,

var socket = io(\'http://127.0.0.1:3001\');
         


        
5条回答
  •  醉梦人生
    2020-12-17 15:49

    If running Express as follows:

    var app = express();
    var server = app.listen(3000);
    

    So to start socket.io, we can do the following:

    var io = require('socket.io').listen(server);
    

    The problem was because of following code:

    var http = require('http').Server(app);
    var io = require('socket.io')(http);
    

    The server object passed to socket.io was not the same as the server object I'm listening on.

提交回复
热议问题