信息 – 未处理的socket.iourl

我试图使用套接字来连接到一个节点服务器我运行本地化,但我一直在'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." 在铬上(使用地址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,以允许两者之间的asynchronous请求。 浏览器要求出于安全原因。

您的服务器似乎正在使用Express。 在Express中启用CORS是非常容易的,这要归功于像cors这样的NPM软件包。 或者,看看这样的一些问题,如: 如何允许CORS?