Socket.on 里面的 socket.on [英] Socket.on inside socket.on

查看:44
本文介绍了Socket.on 里面的 socket.on的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望服务器仅在客户端在另一个套接字上发送消息后才开始侦听来自客户端的某个套接字.我试着像这样编写我的代码:

I want the server to start listening to a certain socket from the client only after the client sends a message on another socket. I tried composing my code like that:

socket.on('listentome', function() {
   socket.on('actualmessage', function() {
     ...
   });
});

但是没有用.有什么建议吗?

However it did not work. Any suggestions?

推荐答案

最简单的解决方案是保持状态,让您知道是否正在等待第二个事件.

The simplest solution would be to keep state, which lets you know if you're awaiting the second event or not.

let waiting = false;

socket.on('listentome', () => waiting = true);

socket.on('actualmessage', () => {
  if (!waiting) return; // if we're not waiting for this event, ignore it

  waiting = false;
  ...
});

这篇关于Socket.on 里面的 socket.on的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆