首页 > 解决方案 > 如何仅在收到授权事件时才调用由其他事件触发的函数?

问题描述

我有一个实现 jwt 授权的 Socket.io 连接。我已经设法发出身份验证令牌并获得响应,但我只想在连接被授权时发出“onClick()”内部的内容。有什么办法可以做到这一点?

const socket = io('http://localhost:8000',{transports: ['websocket']});

socket.on('connect', function () {
  console.log('Connected?');

  socket
    .on('authenticated', function () {
      console.log('Authenticated');
      socket.emit('message', 'hello');
    })
    .emit('authenticate', {token: jwt}) //send the jwt

});


onClick = () => {

    socket.emit('message','button clicked');
}

标签: javascriptnode.jssocket.io

解决方案


推荐阅读