Commit baf1e2f3ed70f568e60e472ed65856c01ccf9a32
1 parent
7f174ff8
1.当websoket一直是连接状态时,不需要一直创建websoket,只需要发送信息确认,关闭时关闭老的websoket对象,断网或是非正常状态关闭,恢复网络后需要重新连接
Showing
2 changed files
with
5 additions
and
5 deletions
src/mes/indexMes/index.js
| @@ -348,8 +348,8 @@ const useIndexMesEvent = props => { | @@ -348,8 +348,8 @@ const useIndexMesEvent = props => { | ||
| 348 | 348 | ||
| 349 | useEffect(() => { | 349 | useEffect(() => { |
| 350 | const connectWs = () => { | 350 | const connectWs = () => { |
| 351 | - if (!wsRef.current) { | ||
| 352 | - console.log("================webSocket不存在,创建webSocket连接======================"); | 351 | + if (!wsRef.current || wsRef.current.readyState === WebSocket.CLOSED || wsRef.current.readyState === WebSocket.CLOSING) { |
| 352 | + console.log("================webSocket不存在或已关闭,创建webSocket连接======================"); | ||
| 353 | props.dispatch({ | 353 | props.dispatch({ |
| 354 | type: "app/createWebSocket", | 354 | type: "app/createWebSocket", |
| 355 | payload: { reStart: true, dispatch: props.dispatch } | 355 | payload: { reStart: true, dispatch: props.dispatch } |
src/models/app.js
| @@ -595,8 +595,8 @@ export default { | @@ -595,8 +595,8 @@ export default { | ||
| 595 | // 清理旧的WebSocket连接和定时器 | 595 | // 清理旧的WebSocket连接和定时器 |
| 596 | let oldWebSocket = yield select(state => state.app.webSocket); | 596 | let oldWebSocket = yield select(state => state.app.webSocket); |
| 597 | if (oldWebSocket) { | 597 | if (oldWebSocket) { |
| 598 | - console.log('发现旧的WebSocket连接,准备清理:', oldWebSocket); | ||
| 599 | - console.log('旧WebSocket连接状态:', oldWebSocket.readyState); | 598 | + // console.log('发现旧的WebSocket连接,准备清理:', oldWebSocket); |
| 599 | + // console.log('旧WebSocket连接状态:', oldWebSocket.readyState); | ||
| 600 | try { | 600 | try { |
| 601 | // 移除所有事件监听器 | 601 | // 移除所有事件监听器 |
| 602 | oldWebSocket.onopen = null; | 602 | oldWebSocket.onopen = null; |
| @@ -609,7 +609,7 @@ export default { | @@ -609,7 +609,7 @@ export default { | ||
| 609 | // 关闭连接 | 609 | // 关闭连接 |
| 610 | if (oldWebSocket.readyState === WebSocket.OPEN || oldWebSocket.readyState === WebSocket.CONNECTING) { | 610 | if (oldWebSocket.readyState === WebSocket.OPEN || oldWebSocket.readyState === WebSocket.CONNECTING) { |
| 611 | oldWebSocket.close(1000, '正常关闭,准备重连'); | 611 | oldWebSocket.close(1000, '正常关闭,准备重连'); |
| 612 | - console.log('已关闭旧的WebSocket连接'); | 612 | + // console.log('已关闭旧的WebSocket连接'); |
| 613 | } | 613 | } |
| 614 | // 断开引用,帮助垃圾回收 | 614 | // 断开引用,帮助垃圾回收 |
| 615 | oldWebSocket = null; | 615 | oldWebSocket = null; |