简体   繁体   中英

info - unhandled socket.io url

I'm trying to use sockets to connect to a Node server I'm running localy but I keep on getting 'info - unhandled socket.io url' on the server side + "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." on chrome (using address 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);

also I'm using python SimpleHTTPServer on port 8000

client code is:

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

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

using https://cdn.socket.io/socket.io-1.3.4.js " version of sockets.io

I'm assuming the html is irrelevant since I don't have any javascript code in it (except references to angular and jquery)

That's a problem with CORS .

Since your web page is on localhost:8000 and the Socket.io server is on localhost:8080 (two different ports, so two different servers), to allow async requests between the two of them you need to enable CORS. Browsers require that for security reasons.

Your server seems to be using Express. Enabling CORS in Express is quite easy, thanks to NPM packages like cors . Alternatively, look at some questions on SO, like: How to allow CORS?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM