Node.js WebSocket Server

I am currently trying to instantiate a push server for new activities in our database. Of course, you will find a lot of information about this topic.

I use: http://static.brandedcode.com/nws-docs/#s6-p1

At the next client implementation:

<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <script src="http://cdn.socket.io/stable/socket.io.js"></script> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script> <title></title> </head> <body> <script type="text/javascript"> (function() { var webSocket = new io.Socket('ws//test', { port: 8080 }); webSocket.connect(); webSocket.on('connect',function() { console.log('Client has connected to the server!'); }); webSocket.on('message',function(data) { console.log('Received a message from the server!',data); }); webSocket.on('disconnect',function() { console.log('The client has disconnected!'); }); window.ws = webSocket; }()); </script> </body> </html> 

The console returns:

 Unexpected response code: 404 XMLHttpRequest cannot load http://ws//test:8080/socket.io/xhr-polling//1303822796984. Origin http://test is not allowed by Access-Control-Allow-Origin. 1303822796984GET http://ws//test:8080/socket.io/xhr-polling//1303822796984 undefined (undefined) 

I do not know the problem.

Thank you for your help.

Congratulations!

+4
source share
3 answers

You are trying to connect directly to the WebSocket server using Socket.io. If you are using only a WebSocket server and not a Socket.io server, you can use the regular HTML5 API to connect to websites.

eg:

 var ws = new WebSocket("ws://domain:port"); ws.onopen = function(){} ws.onmessage = function(m){} ws.onclose = function(){} 

Which browser are you using? WebSockets is currently supported only by Google Chrome. Tests in other browsers will fail.

+6
source

You probably wanted 'ws://push.xxx.binder.test' instead of 'ws//push.xxx.binder.test' (missing colon).

+1
source

change

 var webSocket = new io.Socket('ws//push.xxx.binder.test', { 

to

 var webSocket = new io.Socket('push.xxx.binder.test', { 

You do not need to add a prefix for your domain for socket.io (especially without a colon before slashes). In addition, var webSocket not a good naming convention - socket.io can use not only websockets (even in your mistakes it uses xhr-poliing )

0
source

All Articles