/ / Socket.io (NodeJS) umožňuje CORS - django, node.js, zásuvky, socket.io

Socket.io (NodeJS) umožňuje CORS - django, node.js, sockets, socket.io

Vytváram aplikáciu python (django) s funkciami v reálnom čase, ktoré poskytuje uzol pomocou socket.io. Aplikácia Django beží http://127.0.0.1:8001 a server socket.io zapnutý http://127.0.0.1:8002, Problém je v načítaní socket.io takto do šablóny django:

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

Snažil som sa povoliť CORS v mojom skripte uzlov pomocou „pôvodu“, ale nič nefunguje. Tu je môj kód:

var http = require("http");
var server = http.createServer().listen(8002);
var io = require("socket.io").listen(server);
var cookie_reader = require("cookie");
var querystring = require("querystring");
var redis = require("redis");

//Configure socket.io to store cookie set by Django
io.use(function(){
io.set("authorization", function(data, accept){
if(data.headers.cookie){
data.cookie = cookie_reader.parse(data.headers.cookie);
return accept(null, true);
}
return accept("error", false);
});
io.set("log level", 1);
io.set("origins", "http://127.0.0.1:8001");
});

io.sockets.on("connection", function (socket) {
// Create redis client
client = redis.createClient();

// Subscribe to the Redis events channel
client.subscribe("notifications." + socket.handshake.cookie["sessionid"]);

// Grab message from Redis and send to client
client.on("message", function(channel, message){
console.log("on message", message);
socket.send(message);
});

// Unsubscribe after a disconnect event
socket.on("disconnect", function () {
client.unsubscribe("notifications." + socket.handshake.cookie["sessionid"]);
});
});

Chyba, ktorú mám na prehliadači Chrome:

XMLHttpRequest cannot load http://localhost/socket.io/?EIO=3&transport=polling&t=1433331728612-1086. No "Access-Control-Allow-Origin" header is present on the requested resource. Origin "http://127.0.0.1:8001" is therefore not allowed access. The response had HTTP status code 404.

odpovede:

0 pre odpoveď č. 1

Vo svojom HTML som mal toto:

var socket = io.connect("localhost", {port: 8002});

Zmena na var socket = io.connect("127.0.0.1:8002"); vyriešil problém CORS.