客户端不在 socketio 中发出事件
Client doesn't emit event in socketio
我遵循了 socket.io 测试指南 here 它显示服务器发出一些事件并且客户端将接收它,但我想要相反的方式。客户端应该发送一些数据,服务器会收到它这是我的代码
test("it should send valid file", (done) => {
const id = "userId";
const fileName = __dirname + "/videoTst.webm";
serverSocket.on("receive_video_file", (data) => {
console.log("called");
expect(data.file).toEqual(prepareFile(fileName));
expect(data.id).toEqual(id);
done();
});
sendVideo(clientSocket, prepareFile(fileName), id, done);
});
和sendVideo
函数
const sendVideo = (socket: any, file: any, usrId: string, callback: any) => {
socket.emit("receive_video_file", { file, id: usrId });
};
此结果超时错误,因为事件未收到或可能根本未发出。我在回调函数中添加了一个控制台日志,但它没有被调用。
注意: 当我尝试从服务器发出 emmit 时
serverSocket.emit("hello", {word: "world", file: prepareFile(fileName)});
clientSocket.on("hello", (args: any) => {
console.log(args);
expect(args.word).toEqual("world");
done();
});
成功了!
我肯定在这里遗漏了一些东西。我是 socket.io 的新手,所以我搞不懂。
更新: 当我设置 DEBUG=socket.io:client*,socket.io:server*
时,我发现只发送了 type 0
条消息
$ jest
socket.io:server initializing namespace / +0ms
socket.io:server creating engine.io instance with opts {"path":"/socket.io"} +1ms
socket.io:server attaching client serving req handler +1ms
socket.io:server incoming connection with id uI2s4pGCSIJ5LiuFAAAA +34ms
socket.io:client connecting to namespace / +0ms
socket.io:client writing packet {"type":0,"data":{"sid":"OSg_9WK_p05JeRZCAAAB"},"nsp":"/"} +3ms
socket.io:client client close with reason transport error +137ms
socket.io:server incoming connection with id csCLNa-khLRO0xLNAAAC +1s
socket.io:client connecting to namespace / +1s
socket.io:client writing packet {"type":0,"data":{"sid":"aeyzUguWgcuJFZwFAAAD"},"nsp":"/"} +2ms
socket.io:client client close with reason forced close +4s
FAIL test/unit/SendVideo/SendVideo.test.ts (10.496 s)
原来是maxHttpBufferSize
,我的文件是2MB。当我增加缓冲区大小时它起作用了
我遵循了 socket.io 测试指南 here 它显示服务器发出一些事件并且客户端将接收它,但我想要相反的方式。客户端应该发送一些数据,服务器会收到它这是我的代码
test("it should send valid file", (done) => {
const id = "userId";
const fileName = __dirname + "/videoTst.webm";
serverSocket.on("receive_video_file", (data) => {
console.log("called");
expect(data.file).toEqual(prepareFile(fileName));
expect(data.id).toEqual(id);
done();
});
sendVideo(clientSocket, prepareFile(fileName), id, done);
});
和sendVideo
函数
const sendVideo = (socket: any, file: any, usrId: string, callback: any) => {
socket.emit("receive_video_file", { file, id: usrId });
};
此结果超时错误,因为事件未收到或可能根本未发出。我在回调函数中添加了一个控制台日志,但它没有被调用。
注意: 当我尝试从服务器发出 emmit 时
serverSocket.emit("hello", {word: "world", file: prepareFile(fileName)});
clientSocket.on("hello", (args: any) => {
console.log(args);
expect(args.word).toEqual("world");
done();
});
成功了! 我肯定在这里遗漏了一些东西。我是 socket.io 的新手,所以我搞不懂。
更新: 当我设置 DEBUG=socket.io:client*,socket.io:server*
时,我发现只发送了 type 0
条消息
$ jest
socket.io:server initializing namespace / +0ms
socket.io:server creating engine.io instance with opts {"path":"/socket.io"} +1ms
socket.io:server attaching client serving req handler +1ms
socket.io:server incoming connection with id uI2s4pGCSIJ5LiuFAAAA +34ms
socket.io:client connecting to namespace / +0ms
socket.io:client writing packet {"type":0,"data":{"sid":"OSg_9WK_p05JeRZCAAAB"},"nsp":"/"} +3ms
socket.io:client client close with reason transport error +137ms
socket.io:server incoming connection with id csCLNa-khLRO0xLNAAAC +1s
socket.io:client connecting to namespace / +1s
socket.io:client writing packet {"type":0,"data":{"sid":"aeyzUguWgcuJFZwFAAAD"},"nsp":"/"} +2ms
socket.io:client client close with reason forced close +4s
FAIL test/unit/SendVideo/SendVideo.test.ts (10.496 s)
原来是maxHttpBufferSize
,我的文件是2MB。当我增加缓冲区大小时它起作用了