请问egg-socket.io如何操作房间,socket.io的join、leave方法在egg-socket.io怎么使用?
在controller中使用
await this.ctx.socket.join('room1');
后调用以下语句均无效果
await this.ctx.socket.to('room1').emit('fate', 'test');
await this.ctx.to('room1').emit('fate', 'test');
await this.app.io.to('room1').emit('fate', 'test');
对socket直接发消息是可以的
await this.ctx.socket.emit('fate', 'test');
找到原因了,如果连接指定了命名空间的话,发送房间消息时也必须指定命名空间,我路由上命名空间是/fighting,所以发送房间消息时对应就是this.app.io.of('/fighting').to('room1').emit('fate', 'test');
希望对同样遇到这个问题的朋友有所帮助。
找到原因了,如果连接指定了命名空间的话,发送房间消息时也必须指定命名空间,我路由上命名空间是/fighting,所以发送房间消息时对应就是this.app.io.of('/fighting').to('room1').emit('fate', 'test');
希望对同样遇到这个问题的朋友有所帮助。
请问,指定了命名空间,客户端发送emit的时候,服务端收不到消息。
Most helpful comment
找到原因了,如果连接指定了命名空间的话,发送房间消息时也必须指定命名空间,我路由上命名空间是/fighting,所以发送房间消息时对应就是this.app.io.of('/fighting').to('room1').emit('fate', 'test');
希望对同样遇到这个问题的朋友有所帮助。