[英]JS socket.io client doesn't catch an emitted event
I'm using a Python based socketio server and JS socketio client.我正在使用基于 Python 的 socketio 服务器和 JS socketio 客户端。 The workflow looks roughly like this:
client emits <<action1>>
-> server receives <<action1>>
-> server performs some calculations
-> server emits <<action2>>
-> client receives <<action2>>
It should work in a loop.工作流程大致如下:
client emits <<action1>>
-> server receives <<action1>>
-> server performs some calculations
-> server emits <<action2>>
-> client receives <<action2>>
它应该可以工作在一个循环中。
The problem is that for some reason the last step isn't performed, client doesn't seem to catch action2
event.问题是由于某种原因没有执行最后一步,客户端似乎没有捕获
action2
事件。 I can see that it has been fired from the server logs, they state that: emitting event "action2" to all [/]
我可以看到它已从服务器日志中触发,它们声明:
emitting event "action2" to all [/]
Server:服务器:
@sio.on('action1')
def handle_analysis():
... some calculations ...
sio.emit('action2')
Client:客户:
import { io } from "socket.io-client";
export const socket = io("http://localhost:5550",
{
timeout: 200000
});
async runCalculations() {
console.log('progress1');
let stdoutChunks: string = '';
const getCalculations = () => {
socket.emit('action1');
return new Promise((resolve, reject) => {
socket.on('action2', (msg) => {
stdoutChunks += msg;
resolve(stdoutChunks);
})
});
};
await getCalculations()
console.log('progress2');
return {};
}
progress2
is never logged, because js keeps on awaiting the getCalculations
function. progress2
永远不会被记录,因为 js 一直在等待getCalculations
函数。 Can anyone point where the issue might be?谁能指出问题可能出在哪里? Each calculation takes roughly 50 seconds, I wonder if that might be the cause.
每次计算大约需要 50 秒,我想知道这是否可能是原因。 I used same approach for different events that take much less time and it works perfectly, server communicates with the client without problems.
我对不同的事件使用了相同的方法,这些事件花费的时间要少得多,而且效果很好,服务器与客户端通信没有问题。
I'm not that good/versed with python-socket.io but isn't getCalculations() method defined inside runCalculations() in Client .我对 python-socket.io 不是那么好/精通,但不是在Client的runCalculations()中定义的getCalculations()方法。 So, await getCalculations() won't directly work and also in Server shouldn't sio.emit('action2') also have a second argument like this sio.emit('action2', 'hello') .
因此, await getCalculations()不会直接工作,而且在服务器中也不应该sio.emit('action2')也有像这样的第二个参数sio.emit('action2', 'hello') 。 Hope, this may be useful
希望,这可能有用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.