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

时间:2014-10-17 16:20:22

标签: javascript node.js socket.io

我有以下配置:

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

<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>

好的,当我打电话给http://localhost:3000/blockcallback看起来一切都很完美,但我没有得到这样的状态&#39;致电客户。

我的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

我不明白发生了什么......

此致

1 个答案:

答案 0 :(得分:1)

我认为您的问题是您尚未等待连接...

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