嘿,我正在尝试使用socket.io与跨域.例如,假设域名是:https://client.domain.com和https://server.domain.com.我的客户端代码如下所示:
socket = io.connect("https://server.domain.com"); socket.on("connect", function () { console.log("socketio Connected to server!"); }); socket.emit("test", {"test":"test"});
和服务器端:
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); });
一切正常,消息来回发送.但有些reasone socket.io一直试图使用轮询和错误的域.几乎每一秒我都看到这些要求:
Request URL:https://client.domain.com/socket.io/?EIO=3&transport=polling&t=1452418594321-145 Request Method:GET Status Code:302 OK
为什么socket.io这样做,我怎么能关闭它?
编辑:更有意思的是,如果我注释掉所有与socket.io相关的代码并且只留下它仍然继续制作前面描述的这些HTTP请求.
编辑2:不确定这是否会产生任何差异,但实际上域名不是一个域的子域名.他们是https://clientdomain.com
和https://serverdomain.com
.