最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Not emittingreceiving events with socket.io and node.js - Stack Overflow

programmeradmin0浏览0评论

So I have this very basic socket.io setup that you have probably seen a thousand times already.

Please not that here that the files are served via 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 output I get is

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

in the node console and nothing in the client console. Which is wrong.

I have tried the basic example from the socket.io website and it shows exactly the same behaviour. It logs the emitted data in the node console but nothing else seems to happen.

Edit: Upon further investigation, visiting the site in Firefox creates a different 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"}]}

This looks like the data emitted fromt he client actually reached the server. However, it didnt seem to solve the actual problem: the console.log lines and both the client and the server side arent executed. This output is from Firefox 5.0.1 where it seems to fall back to xhr.

Thanks a lot in advance!

So I have this very basic socket.io setup that you have probably seen a thousand times already.

Please not that here that the files are served via 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 output I get is

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

in the node console and nothing in the client console. Which is wrong.

I have tried the basic example from the socket.io website and it shows exactly the same behaviour. It logs the emitted data in the node console but nothing else seems to happen.

Edit: Upon further investigation, visiting the site in Firefox creates a different 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"}]}

This looks like the data emitted fromt he client actually reached the server. However, it didnt seem to solve the actual problem: the console.log lines and both the client and the server side arent executed. This output is from Firefox 5.0.1 where it seems to fall back to xhr.

Thanks a lot in advance!

Share Improve this question edited Jun 7, 2012 at 16:54 ngmir asked Jun 7, 2012 at 13:33 ngmirngmir 4988 silver badges28 bronze badges 7
  • Why http://127.0.0.1:8080/projectname/server/ and not http://127.0.0.1:8080/ or simply io.connect() ? – alessioalex Commented Jun 7, 2012 at 13:35
  • i cant quite tell, this is more a result of trial and error. this is where the server app.js is. everything else throws a clientside 404 on socket.io.js. maybe it is because I have an apache webserver running on localhost at the same time. – ngmir Commented Jun 7, 2012 at 14:17
  • Are you proxy-ing to Node through Apache or what exactly? – alessioalex Commented Jun 7, 2012 at 14:50
  • not knowingly. i am new to these things. the files are served with apache via some standard port (80 i assume). the client connects to socket.io via port 8080 how you can see. – ngmir Commented Jun 7, 2012 at 14:54
  • Sounds like you can drop projectname/server and just connect to 127.0.0.1:8080. If the client is connecting direct to your node app on port 8080, the location where your app.js is doesn't matter.. it lives at 8080. Which version of Firefox did you use in that second log? Socket.io fell back to xhr polling.. it's not even running web sockets. – Wes Johnson Commented Jun 7, 2012 at 16:35
 |  Show 2 more ments

1 Answer 1

Reset to default 4

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

You should get rid of /projectname/server from your client connect address and reference the client side library properly so you don't get a 404. Whether that's an Apache proxy issue or fixing the script src in your header depends on your current setup. I can't tell from the code you've provided.

PS: Socket.io should serve the client library at http://127.0.0.1:8080/socket.io/socket.io.js, but you might run into a cross-domain origin policy issue by referencing that asset from a document served by your apache server at port 80. Quick fix could be to serve the client lib from your apache server, which is in the socket.io-client module dist folder.

发布评论

评论列表(0)

  1. 暂无评论