【问题标题】:Cross-Origin socket.io connection to express.io server fails与 express.io 服务器的跨域 socket.io 连接失败
【发布时间】:2014-10-03 02:46:05
【问题描述】:

我正在尝试设置 socket.io 以连接到具有不同来源的流服务器。

服务器是作为大型设置的一部分的专用流服务器,基于 Express.io:

var express = require('express.io'),
    config  = require('./config'),
    app     = express().http().io();

app.io.set('origin','*:*');

app.all('*', function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "X-Requested-With, Content-Type");
  res.header("Access-Control-Allow-Methods", 'GET, PUT, POST, DELETE, HEAD', 'OPTIONS');

  // Tried with and without this header
  res.header('Access-Control-Allow-Credentials', false);

  next();
});

app.io.route('ready', function(req) {
    req.io.emit('talk', {
      message: 'io event from an io route on the server'
    });
});

app.listen(4000, function(){
  console.log('Listening on port ' + config.port);
});

尝试打开连接会导致以下错误:

http://localhost:4000/?EIO=2&transport=polling&t=1407621822859-65. 
A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin' header when the credentials flag is true. 
Origin 'http://localhost:8080' is therefore not allowed access.

如何建立这样的连接?

谢谢!

【问题讨论】:

    标签: node.js sockets express socket.io express.io


    【解决方案1】:

    这个问题并没有完全解决,但我能够通过恢复到 一个常规的 express.js+socket.io 组合。

    即使对于使用此设置的跨域请求,标头问题似乎也会平静下来。

    【讨论】:

      猜你喜欢
      • 2013-01-06
      • 1970-01-01
      • 2013-12-04
      • 2012-07-16
      • 2012-02-16
      • 1970-01-01
      • 2021-08-22
      • 2019-12-09
      • 2017-11-09
      相关资源
      最近更新 更多