简体   繁体   English

node.js socket.io发出不起作用

[英]node.js socket.io emit not working

I've got the following config: 我有以下配置:

app.set('port', process.env.PORT || 3000);
var httpserver = http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

var io = require('socket.io')(httpserver);

app.all('/blockcallback', function(req, res){

    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");

    getTxSender(req.query.tx,function(ref_wallet){
          (..)
   });
   io.emit('status', {'message': 'done'});
   res.send('*ok*');
});

index.html 的index.html

<script src="http://localhost:3000/socket.io/socket.io.js"></script>
<script>
                    var socket = io.connect('http://localhost:3000');
                        socket.on('status', function(datos){
                            alert('received');  
                        });
</script>

ok when i call http://localhost:3000/blockcallback looks like everything works perfectly, but i am not getting the 'status' call on the clientside. 好的,当我调用http://localhost:3000/blockcallback看起来一切正常,但是我没有在客户端收到“状态”调用。

my js console shows: 我的js控制台显示:

Error in event handler for extension.onRequest: undefined
Stack trace: undefined localhost/:1 
Error in event handler for extension.onRequest: undefined
Stack trace: undefined extensions::uncaught_exception_handler:9
Stack trace: undefined extensions::uncaught_exception_handler:9

I don't understand what's happening... 我不明白发生了什么...

Regards, 问候,

I think your problem is you haven't waited for the connection yet... 我认为您的问题是您尚未等待连接...

io.on('connection', function (socket) {
  socket.emit('status', { hello: 'world' });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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