信息-未处理的socket.io url

info - unhandled socket.io url

本文关键字:io url socket 未处理 信息      更新时间:2024-02-01

我正在尝试使用套接字连接到本地运行的Node服务器但我一直在服务器端获取'info - unhandled socket.io url'+chrome上的"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。出于安全原因,浏览器需要这样做。

您的服务器似乎正在使用Express。由于像CORS这样的NPM包,在Express中启用CORS非常容易。或者,看看SO上的一些问题,比如:如何允许CORS?