通过websocket并使用轮询后备客户端和CORS进行socket.io

时间:2018-07-07 11:27:24

标签: node.js express socket.io cors engine.io

我们的index.html加载了main.js,这将加载engine.io,然后连接到https://dev.ourcompany.com:3000。该套接字服务器是用 express socket.io Node.js 编写的。现在我们的团队在其index.html上提供localhost,浏览器将拒绝该连接。可以添加标题有帮助吗?如果是,在何处添加标题?服务index.html或在套接字服务器端(express + socket.io)时?我都尝试过,但没有用

客户(index.html):

var socket = eio('https://dev.company.com:3000');
socket.on('open', function() {
    socket.on('message', function(data){});
    socket.on('close', function(){});
});

服务器(express + socket.io):

server = httpsServer.listen(process.env.NODE_SOCKET_SSL_PORT);
var io = require('socket.io').listen(server);
io.set('transports', ['websocket', 'polling']);

我尝试在之后添加此内容(没有帮助):

app.get('/*',function(req,res,next) { // CORS  for localhost
    res.header('Access-Control-Allow-Origin' , '*')
    next()
}); 

也尝试过此操作(curl显示添加的标题):

app.get('/', function (req, res) {
    console.log('Adding CORS header')
    res.header('Access-Control-Allow-Origin' , '*')
  res.sendFile(__dirname + '/index_socket.html');
});

如何可靠地解决此问题?重要的是,我们的团队有很多人,不可能要求所有人重新编译firefox,调整其主机文件或安装代理。

0 个答案:

没有答案