Socket.io从服务器到客户端的通信没有发生

时间:2016-09-27 18:02:32

标签: node.js angular socket.io

我正在尝试使用socket.io进行非常简单的实时通知。由于某种原因,我无法从服务器接收数据或从事件发送到客户端但是从客户端到服务器是。让我展示我的代码:

客户端

 ngOnInit() {

    this.socket.on('connect', function (res: any) {
        console.log('Socket.io is connected on client side!'); // it shows on client console
    });

    this.socket.on('alarmsreceived', function (res: any) {
        console.log(res + ' i am here now'); // is not firing 
    });

}

   // this method fires from a click button
   objectStatus = () => {

    this.socket.emit('alarmsystem', 'i am client going to server');

}

服务器

var io = require('socket.io').listen(server);
var connections = [];

io.of('/api/v1/monitoring').on('connect', function(socket){
connections.push(socket);
console.log('Connected %s sockets', connections.length); // i see connection on cmd

socket.on('disconnect', function() {
    connections.splice(connections.indexOf(socket), 1);
    console.log('Connected %s sockets', connections.length);
});

socket.on('alarmsystem', function(res) {
    console.log(res); // this shows me the message from client
    io.sockets.emit('alarmsreceived', 'I am server heading to client');
});
})

看起来非常简单,但没有触发客户端事件。有人可以帮我解决我在这里做错了什么吗?提前致谢

0 个答案:

没有答案