信息 - 未处理 socket.io url

info - unhandled socket.io url

我正在尝试使用套接字连接到我在 运行 本地的节点服务器 但我一直在服务器端获得 'info - unhandled socket.io url' + "XMLHttpRequest cannot load http://localhost:8080/socket.io/?EIO=3&transport=polling&t=1424356590540-0. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8000' is therefore not allowed access." chrome(使用地址 localhost:8000/index.html

// top six lines of code to see setup
var express = require('express'), 
app = express(), 
http = require('http'),
server = http.createServer(app),
io = require('socket.io').listen(server);

server.listen(8080);

我还在端口 8000 上使用 python SimpleHTTPServer

客户代码是:

var socket = io.connect('http://localhost:8080');

socket.on('connection', function(){
    console.log("connected")
});

使用 https://cdn.socket.io/socket.io-1.3.4.js" 版本的 sockets.io

我假设 html 是无关紧要的,因为我没有任何 javascript 代码(除了对 angular 和 jquery 的引用)

CORS 有问题。

因为你的网页在 localhost:8000 而 Socket.io 服务器在 localhost:8080(两个不同的端口,所以两个不同的服务器),允许两者之间的异步请求他们你需要启用 CORS。出于安全原因,浏览器需要这样做。

您的服务器似乎正在使用 Express。借助像 cors. Alternatively, look at some questions on SO, like: How to allow CORS?

这样的 NPM 包,在 Express 中启用 CORS 非常容易