Socket.io is constantly trying to use polling on the wrong address

Hi, I am trying to use socket.io with cross domains. For example, suppose the domains are: https://client.domain.com and https://server.domain.com . My client side code is as follows:

socket = io.connect("https://server.domain.com"); socket.on("connect", function () { console.log("socketio Connected to server!"); }); socket.emit("test", {"test":"test"}); 

And server side:

 var fs = require('fs'); var https = require('https'); var express = require('express'); var socketIO = require('socket.io'); var app = express(); // Settings var serverOptions = { key: fs.readFileSync('privkey1.pem'), cert: fs.readFileSync('cert1.pem') }; var serverPort = 443; // Logic var server = https.createServer(serverOptions, app); var io = socketIO(server); io.on('connection', function(socket) { socket.on("test", function(data) { console.log(data); }); }); server.listen(serverPort, function() { console.log('server up and running at %s port', serverPort); }); 

Everything works, messages are sent back and forth. But some reasone socket.io continue to try polling and the wrong domain. Almost every second I see this request:

 Request URL:https://client.domain.com/socket.io/?EIO=3&transport=polling&t=1452418594321-145 Request Method:GET Status Code:302 OK 

Why does the .io socket do this and how can I disable it?

EDIT: more interestingly, if I comment on all the code that needs to be done with socket.io and leave it only in <script src="https://server.domain.com/socket.io/socket.io.js"></script> , it still continues to make these HTTP requests described earlier.

EDIT2: Not sure if this will make a difference, but in reality domains are not subdomains of the same domain. They are https://clientdomain.com and https://serverdomain.com .

+6
source share
2 answers

If you use nginx , I think your problem comes from how you configured it. You absolutely need the following things in your location block, as indicated in socket.io docs

Make sure these headers are set correctly:

 location / { proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $host; ... } 

Socket.IO does a great job with CORS, so I doubt the problem is here.

0
source

The request you see is that you are serving the client from https://client.domain.com .

 Request URL:https://client.domain.com/socket.io/?EIO=3&transport=polling&t=1452418594321-145 

is just a client receiving files for socket.io so that it can use the io object. This is not related to connecting to https://server.domain.com , as it is a request for socket.io files that are served from https://client.domain.com .

Your code connects the client to https://server.domain.com using the socket.io source files obtained from https://client.domain.com via https // server.domain.com. After receiving files from the server, they serve clients through its connection to https://client.domain.com .

Essentially, a request is just code, a way of saying that it is requesting socket.io files from the client domain, which come from the server domain but are sent through the client domain to the actual client (and not the client).

0
source

All Articles