javascript - 如何仅在收到授权事件时才调用由其他事件触发的函数?
问题描述
我有一个实现 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');
}
解决方案
推荐阅读
- python - 为什么 PyCharm 在运行任何代码后都会给出相同的结果?
- java - Maven依赖插件
- javascript - 获取 POST 请求保存数据,但在使用 Google 表单将数据保存到 Google 电子表格时返回 ok 作为 false
- reactjs - 无法重新导出 directory.js 中的组件
- google-apps-script - Highlight duplicates and copy to new sheet
- javascript - 有没有办法防止根据条件触发事件?
- clickhouse - 从 IDE DataGrip 连接到 ClickHouse 时出现“未知设置 session_id”
- javascript - 指定的值“NaN”无法解析,或者在 Jquery 中超出范围
- visual-studio - 为什么我的汇编代码在 Visual Studio 上显示错误消息?
- java - 段落中单词的频率