所以我使用express设置socket并将其与react结合使用。我注意到我发送的第一个发射将在几秒到十秒之间的任何时间延迟。
然而,虽然第一次发射是"悬挂"我可以发送"强制"第一个发射立即发送。
就像第一次发射有延迟然后服务器"醒来"当第二次发射时。
然后重复模式。
这是我的代码:
(客户端最顶层的React组件)
testerSocketButton(){
var sendDate = new Date();
var stepCounter = this.state.stepCounter;
console.log("testerSocket Called @: " + sendDate + " // " + stepCounter);
this.setState((prevState)=>{
return {stepCounter: prevState.stepCounter + 1}
}, ()=>{
console.log("test-message", new Date().toString() + " // Press # " + stepCounter);
socket.emit("test-message", sendDate.toString() + " // Press # " + stepCounter);
});
}
render(){
return(
<div>
<div onClick={this.testerSocketButton}> TEST SOCKET </div>
</div>
);
(服务器app.js)
const express = require("express");
const http = require("http");
const socketIo = require("socket.io");
const port = 8000;
const app = require("express")();
var server = http.createServer(app);
var io = require('socket.io')(server);
io.on("connection", socket => {
console.log("Client connected");
socket.on("disconnect", () => {
console.log("Client disconnected");
});
socket.on("test-message", testMsg=>{console.log("Test message received.\n Date from client: " + testMsg + "\n Date from server: " + new Date())});
});
server.listen(port, () => console.log(`Listening on port ${port}`));
编辑: 好吧,我还用计数器调整了我的测试,看看这些消息是否有故障。我的假设是错误的,但事实并非如此。他们仍然被推迟了#34;虽然我除了强制发射另一个发射,它似乎立即推动下一个发射。
编辑2:我尝试了四次,基本上连续四次快速点击我的按钮触发器。你会发现每四次都会有延迟。
调试模式结果:
engine:ws received "42["test-message","Mon Jan 29 2018 22:39:23 GMT-0800 (Pacific Standard Time) // Press # 1"]" +11s
engine:socket packet +1ms
socket.io-parser decoded 2["test-message","Mon Jan 29 2018 22:39:23 GMT-0800 (Pacific Standard Time) // Press # 1"] as {"type":2,"nsp":"/","data":["test-message","Mon Jan 29 2018 22:39:23 GMT-0800 (Pacific Standard Time) // Press # 1"]} +11s
socket.io:socket got packet {"type":2,"nsp":"/","data":["test-message","Mon Jan 29 2018 22:39:23 GMT-0800 (Pacific Standard Time) // Press # 1"]} +25s
socket.io:socket emitting event ["test-message","Mon Jan 29 2018 22:39:23 GMT-0800 (Pacific Standard Time) // Press # 1"] +1ms
socket.io:socket dispatching an event ["test-message","Mon Jan 29 2018 22:39:23 GMT-0800 (Pacific Standard Time) // Press # 1"] +0ms
Test message received.
Date from client: Mon Jan 29 2018 22:39:23 GMT-0800 (Pacific Standard Time) // Press # 1
Date from server: Mon Jan 29 2018 22:39:37 GMT-0800 (Pacific Standard Time)
engine:ws received "2" +14s
服务器结果:
Test message received.
Test message received.
Date from client: Mon Jan 29 2018 19:57:41 GMT-0800 (Pacific Standard Time) // Press # 0
Date from server: Mon Jan 29 2018 19:57:41 GMT-0800 (Pacific Standard Time)
Test message received.
Date from client: Mon Jan 29 2018 19:57:41 GMT-0800 (Pacific Standard Time) // Press # 1
Date from server: Mon Jan 29 2018 19:57:42 GMT-0800 (Pacific Standard Time)
Test message received.
Date from client: Mon Jan 29 2018 19:57:42 GMT-0800 (Pacific Standard Time) // Press # 2
Date from server: Mon Jan 29 2018 19:57:42 GMT-0800 (Pacific Standard Time)
Test message received.
Date from client: Mon Jan 29 2018 19:57:42 GMT-0800 (Pacific Standard Time) // Press # 3
Date from server: Mon Jan 29 2018 19:57:48 GMT-0800 (Pacific Standard Time)
Test message received.
Date from client: Mon Jan 29 2018 19:57:55 GMT-0800 (Pacific Standard Time) // Press # 4
Date from server: Mon Jan 29 2018 19:57:55 GMT-0800 (Pacific Standard Time)
Test message received.
Date from client: Mon Jan 29 2018 19:57:55 GMT-0800 (Pacific Standard Time) // Press # 5
Date from server: Mon Jan 29 2018 19:57:55 GMT-0800 (Pacific Standard Time)
Test message received.
Date from client: Mon Jan 29 2018 19:57:55 GMT-0800 (Pacific Standard Time) // Press # 6
Date from server: Mon Jan 29 2018 19:57:56 GMT-0800 (Pacific Standard Time)
Test message received.
Date from client: Mon Jan 29 2018 19:57:56 GMT-0800 (Pacific Standard Time) // Press # 7
Date from server: Mon Jan 29 2018 19:58:13 GMT-0800 (Pacific Standard Time)
Test message received.
Date from client: Mon Jan 29 2018 19:58:16 GMT-0800 (Pacific Standard Time) // Press # 8
Date from server: Mon Jan 29 2018 19:58:16 GMT-0800 (Pacific Standard Time)
Test message received.
Date from client: Mon Jan 29 2018 19:58:16 GMT-0800 (Pacific Standard Time) // Press # 9
Date from server: Mon Jan 29 2018 19:58:17 GMT-0800 (Pacific Standard Time)
Test message received.
Date from client: Mon Jan 29 2018 19:58:17 GMT-0800 (Pacific Standard Time) // Press # 10
Date from server: Mon Jan 29 2018 19:58:17 GMT-0800 (Pacific Standard Time)
Test message received.
Date from client: Mon Jan 29 2018 19:58:17 GMT-0800 (Pacific Standard Time) // Press # 11
Date from server: Mon Jan 29 2018 19:58:38 GMT-0800 (Pacific Standard Time)
Test message received.
Date from client: Mon Jan 29 2018 19:58:43 GMT-0800 (Pacific Standard Time) // Press # 12
Date from server: Mon Jan 29 2018 19:58:43 GMT-0800 (Pacific Standard Time)
Test message received.
Date from client: Mon Jan 29 2018 19:58:43 GMT-0800 (Pacific Standard Time) // Press # 13
Date from server: Mon Jan 29 2018 19:58:43 GMT-0800 (Pacific Standard Time)
Test message received.
Date from client: Mon Jan 29 2018 19:58:43 GMT-0800 (Pacific Standard Time) // Press # 14
Date from server: Mon Jan 29 2018 19:58:43 GMT-0800 (Pacific Standard Time)
Test message received.
Date from client: Mon Jan 29 2018 19:58:43 GMT-0800 (Pacific Standard Time) // Press # 15
Date from server: Mon Jan 29 2018 19:58:48 GMT-0800 (Pacific Standard Time)
客户结果:
testerSocket Called @: Mon Jan 29 2018 19:57:41 GMT-0800 (Pacific Standard Time) // 0
App.js:297 test-message Mon Jan 29 2018 19:57:41 GMT-0800 (Pacific Standard Time) // Press # 0
App.js:293 testerSocket Called @: Mon Jan 29 2018 19:57:41 GMT-0800 (Pacific Standard Time) // 1
App.js:297 test-message Mon Jan 29 2018 19:57:41 GMT-0800 (Pacific Standard Time) // Press # 1
App.js:293 testerSocket Called @: Mon Jan 29 2018 19:57:42 GMT-0800 (Pacific Standard Time) // 2
App.js:297 test-message Mon Jan 29 2018 19:57:42 GMT-0800 (Pacific Standard Time) // Press # 2
App.js:293 testerSocket Called @: Mon Jan 29 2018 19:57:42 GMT-0800 (Pacific Standard Time) // 3
App.js:297 test-message Mon Jan 29 2018 19:57:42 GMT-0800 (Pacific Standard Time) // Press # 3
App.js:293 testerSocket Called @: Mon Jan 29 2018 19:57:55 GMT-0800 (Pacific Standard Time) // 4
App.js:297 test-message Mon Jan 29 2018 19:57:55 GMT-0800 (Pacific Standard Time) // Press # 4
App.js:293 testerSocket Called @: Mon Jan 29 2018 19:57:55 GMT-0800 (Pacific Standard Time) // 5
App.js:297 test-message Mon Jan 29 2018 19:57:55 GMT-0800 (Pacific Standard Time) // Press # 5
App.js:293 testerSocket Called @: Mon Jan 29 2018 19:57:55 GMT-0800 (Pacific Standard Time) // 6
App.js:297 test-message Mon Jan 29 2018 19:57:55 GMT-0800 (Pacific Standard Time) // Press # 6
App.js:293 testerSocket Called @: Mon Jan 29 2018 19:57:56 GMT-0800 (Pacific Standard Time) // 7
App.js:297 test-message Mon Jan 29 2018 19:57:56 GMT-0800 (Pacific Standard Time) // Press # 7
App.js:293 testerSocket Called @: Mon Jan 29 2018 19:58:16 GMT-0800 (Pacific Standard Time) // 8
App.js:297 test-message Mon Jan 29 2018 19:58:16 GMT-0800 (Pacific Standard Time) // Press # 8
App.js:293 testerSocket Called @: Mon Jan 29 2018 19:58:16 GMT-0800 (Pacific Standard Time) // 9
App.js:297 test-message Mon Jan 29 2018 19:58:16 GMT-0800 (Pacific Standard Time) // Press # 9
App.js:293 testerSocket Called @: Mon Jan 29 2018 19:58:17 GMT-0800 (Pacific Standard Time) // 10
App.js:297 test-message Mon Jan 29 2018 19:58:17 GMT-0800 (Pacific Standard Time) // Press # 10
App.js:293 testerSocket Called @: Mon Jan 29 2018 19:58:17 GMT-0800 (Pacific Standard Time) // 11
App.js:297 test-message Mon Jan 29 2018 19:58:17 GMT-0800 (Pacific Standard Time) // Press # 11
App.js:293 testerSocket Called @: Mon Jan 29 2018 19:58:43 GMT-0800 (Pacific Standard Time) // 12
App.js:297 test-message Mon Jan 29 2018 19:58:43 GMT-0800 (Pacific Standard Time) // Press # 12
App.js:293 testerSocket Called @: Mon Jan 29 2018 19:58:43 GMT-0800 (Pacific Standard Time) // 13
App.js:297 test-message Mon Jan 29 2018 19:58:43 GMT-0800 (Pacific Standard Time) // Press # 13
App.js:293 testerSocket Called @: Mon Jan 29 2018 19:58:43 GMT-0800 (Pacific Standard Time) // 14
App.js:297 test-message Mon Jan 29 2018 19:58:43 GMT-0800 (Pacific Standard Time) // Press # 14
App.js:293 testerSocket Called @: Mon Jan 29 2018 19:58:43 GMT-0800 (Pacific Standard Time) // 15
App.js:297 test-message Mon Jan 29 2018 19:58:43 GMT-0800 (Pacific Standard Time) // Press # 15
答案 0 :(得分:0)
这是Windows 10中的socket io和uWebSocket问题,此处为其他人提出疑问:this
当前的解决方法:
解决方法是在选项中将wsEngine更改为ws: const io = require(&#39; socket.io&#39;)(httpServer,{wsEngine:&#39; ws&#39;});