【问题标题】:Cross-Origin Request Blocked on socket io with express js使用 express js 在套接字 io 上阻止跨域请求
【发布时间】:2014-05-12 12:43:39
【问题描述】:

我在 Django 中使用 node js、expressjs、socketio、redis。

节点服务器:

const PORT = 8008;
const HOST = 'localhost';

var express = require('express'),
    http = require('http'),
    server = http.createServer(app);

var app = express();

const redis = require('redis');

log('info', 'connected to redis server');

const io = require('socket.io');

if (!module.parent) {
    server.listen(PORT, HOST);
    const socket = io.listen(server);

    socket.on('connection', function(client) {
        const subscribe = redis.createClient(6379, '127.0.0.1')
        subscribe.subscribe('test');

        subscribe.on("message", function(pattern, channel, message) {
            client.send(channel, message);
            log('msg', "received from channel #" + channel + " : " + message);
        });

    });

客户:

<script src="http://localhost:8008/socket.io/socket.io.js"></script>

<script type="text/javascript">

var socket = io.connect("http://localhost:8008/");

      socket.on('connect', function(data){
        socket.emit('subscribe', {channel:'test'});
      });


      socket.on('message', function (data) {
        console.log('received a message: ', data);

      });

服务器正在向通道发送消息,但是当它在客户端上加载时,我收到以下错误:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:8008/socket.io/1/?t=1399898337175. This can be fixed by moving the resource to the same domain or enabling CORS.

【问题讨论】:

    标签: javascript node.js express socket.io


    【解决方案1】:

    您还可以配置您的套接字服务器以启用通配符来源。

    io.configure('development', function(){
        io.set('origins', '*:*');
    }
    

    或者

    io.set('origins', '*:*');
    

    查看https://github.com/LearnBoost/Socket.IO/wiki/Configuring-Socket.IO了解更多信息

    【讨论】:

    • 我遇到了同样的错误,但似乎我使用的是外部 ip 访问站点,但 io.connect... 中的 'localhost'。两者都使用相同的修复它。
    【解决方案2】:

    您必须在服务器端启用 cors。

    将此添加到您的节点代码中

    // Enables CORS
    var enableCORS = function(req, res, next) {
        res.header('Access-Control-Allow-Origin', '*');
        res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
        res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With, *');
    
            // intercept OPTIONS method
        if ('OPTIONS' == req.method) {
            res.send(200);
        } else {
            next();
        };
    };
    
    app.configure(function() {
        // enable CORS!
        app.use(enableCORS);
    
    });
    

    【讨论】:

    • io.set 返回此错误TypeError: Object #&lt;Object&gt; has no method 'set' 和下面的说TypeError: Object function (req, res, next) { app.handle(req, res, next); } has no method 'configure'
    • 是的,第二个选项抛出TypeError: Object function (req, res, next) { app.handle(req, res, next); } has no method 'configure'
    猜你喜欢
    • 2018-11-30
    • 1970-01-01
    • 2021-10-03
    • 2023-03-03
    • 2016-05-19
    • 2018-03-28
    • 2017-09-12
    • 2021-02-19
    • 1970-01-01
    相关资源
    最近更新 更多