Muhammet Arslan
Muhammet Arslan

Reputation: 985

socket.io and node.js gives error on localhost

I have a development server working on 192.168.1.22 and i want to run node.js on it but it gives some error. I want to only test some cases but even can't run it. After i run it i will fetch my onclick button and from node.js sent that post to my php file. and return data from php to node.js and node.js to website.

my js :

var http = require('http'),  
    io = require('socket.io'),

server = http.createServer(function(req, res){
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write('<h1>Sample server created with NodeJS.</h1>');
    res.end();
});
server.listen(8001);

// socket.io 
var socket = io.listen(server); 
socket.on('connection', function(client){ 
  client.send('Hello client'); 
  client.on('message', function(){
     client.send((new Date()).getTime());
  })
}); 

My html:

  <script type="text/javascript" src="http://cdn.socket.io/stable/socket.io.js"></script>
  <script type="text/javascript">
    var socket = new io.Socket(null, {port: 8001});

    socket.connect();
    socket.on('message', function(message){
        document.getElementById('divTime').innerHTML = message;
    });     
    function GetServerTime() {
        socket.send('');
    }  
  </script>

Error :

WebSocket connection to 'ws://192.168.1.22:8001/socket.io/websocket' failed: Connection closed before receiving a handshake response socket.io.js:378
XMLHttpRequest cannot load http://192.168.1.22:8001/socket.io/xhr-polling//1401170092339. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://192.168.1.22:8000' is therefore not allowed access. (index):1
WebSocket connection to 'ws://192.168.1.22:8001/socket.io/websocket' failed: Connection closed before receiving a handshake response socket.io.js:378
XMLHttpRequest cannot load http://192.168.1.22:8001/socket.io/xhr-polling//1401170102339. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://192.168.1.22:8000' is therefore not allowed access. (index):1
WebSocket connection to 'ws://192.168.1.22:8001/socket.io/websocket' failed: Connection closed before receiving a handshake response 

Upvotes: 1

Views: 2092

Answers (1)

dylants
dylants

Reputation: 23340

From the error, it looks like you're trying to connect to this server from another port:

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://192.168.1.22:8000' is therefore not allowed access. (index):1

The server code you pasted above (labeled "my js") looks fine. I'm able to run the same code locally once I've installed the socket.io package. So maybe the problem isn't starting that server code, but rather connecting to it once its started? And if so, the error you have above points to a same-origin policy problem. If you host the HTML code from within the node app, running on the same port, you shouldn't have this issue.

Upvotes: 1

Related Questions