Do not emit / receive events with socket.io and node.js

So, I have this very basic installation of socket.io, which you have probably seen a thousand times already.

Please, not that files were transferred through apache.

server (app.js)

var io = require('socket.io').listen(8080); io.sockets.on('connection', function(socket){ socket.emit('server ready', {msg: 'hi'}) ; socket.on('random event', function(data) { console.log('received'); }) }); 

and client

 $(document).ready(function() { var socket = io.connect('http://127.0.0.1:8080/projectname/server/'); socket.on('server ready', function(data){ console.log('server ready!'); }); socket.emit('random event', {hurr: 'durr'}); }); 

However, the only way out I get is

  debug - websocket writing 5:::{"name":"server ready","args":[{"msg":"hi"}]} 

in the node console and nothing in the client console. It is not right.

I tried the basic example from socket.io and it shows exactly the same behavior. It logs the emitted data in the node console, but nothing else happens.

Edit: upon further research, visiting a site in Firefox generates another output in the node console:

 info - handshake authorized 178702677759276313 debug - setting request GET /socket.io/1/xhr-polling/178702677759276313?t=1339080239192 debug - setting poll timeout debug - client authorized for debug - clearing poll timeout debug - xhr-polling writing 1:: debug - set close timeout for client 178702677759276313 debug - xhr-polling received data packet  17 1::/stock/server/ 66 5::/stock/server/:{"name":"random event","args":[{"hurr":"durr"}]} debug - setting request GET /socket.io/1/xhr-polling/178702677759276313?t=1339080239263 debug - setting poll timeout debug - clearing poll timeout debug - xhr-polling writing 5:::{"name":"server ready","args":[{"msg":"hi"}]} 

It looks like the data received from the client actually reached the server. However, this problem does not seem to be resolved: the console.log lines and the client and server executable are not running. This output is from Firefox 5.0.1 , where it seems to be returning to xhr.

Many thanks!

+4
source share
1 answer

If your projectname is stock , then this is your problem. Socket.IO thinks you are using namespace . You can see this in the xhr-polling received data packet log line. console.log never called since you are not listening on this server-side namespace.

You should get rid of /projectname/server from your client connection address and specify the library on the client side correctly so that you do not get 404. Regardless of whether the Apache proxy problem or the script src fix in your header depends on your current setting. I can not tell from the code you provided.

PS: Socket.io should serve the client library at http://127.0.0.1:8080/socket.io/socket.io.js , but you may encounter a cross-domain policy issue by specifying this asset from a document served by your apache server on port 80. A quick fix may be to service the client library from your apache server, which is located in the socket.io-client module dist folder.

+3
source

Source: https://habr.com/ru/post/1416583/


All Articles