0

语境

  • 12(测试)React Native 设备通过 WebSockets 连接到我们的服务器。
  • 随机一些设备在白天停止接收任何服务器活动。
  • 这些设备将陷入重新连接循环。
    • 以下操作将使设备脱离循环:
      • 重新启动设备上的客户端应用程序。
      • 调用stompSocket.deactivate()后跟stompSocket.activate()
  • 在循环时,每次客户端尝试连接到服务器时,我们的服务器都会注册。
    • 在深入研究我们的服务器日志后,我们可以得出以下结论:
      • 客户端的CONNECT消息被服务器接收。
      • 服务器指示客户端是CONNECTED.
      • 大约 35 秒后,服务器指示客户端是DISCONNECTED
  • 服务器上以及客户端上的心跳设置为10000, 10000
  • 客户使用@stomp/stompjs@6.1.2
  • 在本期中,我将放大其中一台设备的日志。所有设备的问题日志都相似。

附件

目录

  1. StompJS 客户端配置
  2. 重连循环的客户端日志
  3. 重新启动服务器应用程序后成功重新连接的客户端日志

1. StompJS 客户端配置

const stompSocket: Client = new Client({
    brokerURL: WS_URL,
    appendMissingNULLonIncoming: true,
    forceBinaryWSFrames: true,
    reconnectDelay: 5000,
    connectionTimeout: 10000,
});

stompSocket.onConnect = async (): Promise<void> => {
    stompSocket.subscribe(`/topic/${topic}/messages`, async (message: IMessage): Promise<void> => {
        // Handle messages
        ...
    }
}

stompSocket.onChangeState = async (state) => {
    logDebug(`onChangeState ${JSON.stringify(state)}`, 'WS');
};

stompSocket.onWebSocketError = async (error: any) => {
    logError(`onWebSocketError ${JSON.stringify(error)}`, 'WS');
};

stompSocket.onStompError = async (error: any) => {
    logError(`onStompError ${JSON.stringify(error)}`, 'WS');
};

stompSocket.onWebSocketClose = async (evt: any) => {
    logWarn(`onWebSocketClose(): ${JSON.stringify(evt)}`, 'WS');
};

stompSocket.onDisconnect = async (receipt: any) => {
    logWarn(`onDisconnect ${JSON.stringify(receipt)}`, 'WS');
};
    

2.重连循环日志(客户端)

  • 一个客户端停止从服务器接收活动,即使服务器CONNECT从客户端接收到所有消息。
  • 我无法弄清楚为什么在触发 10000 毫秒超时并且连接试图关闭之后CONNECT发送消息。
// No previous logs in the past couple hours.
09:14:33 STOMP did not receive server activity for the last 26911ms
09:14:33 STOMP Issuing close on the websocket
09:14:43 STOMP did not receive server activity for the last 36919ms
09:14:43 STOMP Issuing close on the websocket
09:14:53 STOMP did not receive server activity for the last 46919ms
09:14:53 STOMP Issuing close on the websocket
09:15:03 STOMP did not receive server activity for the last 56923ms
09:15:03 STOMP Issuing close on the websocket
09:15:13 STOMP did not receive server activity for the last 66927ms
09:15:13 STOMP Issuing close on the websocket
09:15:23 STOMP did not receive server activity for the last 76931ms
09:15:23 STOMP Issuing close on the websocket
09:15:33 STOMP did not receive server activity for the last 86934ms
09:15:33 STOMP Issuing close on the websocket
09:15:33 WS onWebSocketError {"isTrusted":false,"message":"Socket closed"}
09:15:33 STOMP Connection closed to wss://example.com/websocket
09:15:33 WS onWebSocketClose {"isTrusted":false,"message":"Socket closed"}
09:15:33 STOMP STOMP: scheduling reconnection in 5000ms
09:15:38 STOMP Opening Web Socket...
09:15:48 STOMP Connection not established in 10000ms, closing socket
09:15:48 STOMP Issuing close on the websocket
09:15:49 STOMP Web Socket Opened...
09:15:49 STOMP
    >>> CONNECT
    accept-version:1.0,1.1,1.2
    heart-beat:10000,10000

09:16:26 STOMP Connection closed to wss://example.com/websocket
09:16:26 WS onWebSocketClose {"isTrusted":false,"code":1002,"reason":""}
09:16:26 STOMP STOMP: scheduling reconnection in 5000ms
09:16:31 STOMP Opening Web Socket...
09:16:41 STOMP Connection not established in 10000ms, closing socket
09:16:41 STOMP Issuing close on the websocket
09:16:41 STOMP Web Socket Opened...
09:16:41 STOMP
    >>> CONNECT
    accept-version:1.0,1.1,1.2
    heart-beat:10000,10000

09:17:16 STOMP Connection closed to wss://example.com/websocket
09:17:16 WS onWebSocketClose {"isTrusted":false,"code":1002,"reason":""}
09:17:16 STOMP STOMP: scheduling reconnection in 5000ms
09:17:21 STOMP Opening Web Socket...
09:17:31 STOMP Connection not established in 10000ms, closing socket
09:17:31 STOMP Issuing close on the websocket
09:17:31 STOMP Web Socket Opened...
09:17:31 STOMP
    >>> CONNECT
    accept-version:1.0,1.1,1.2
    heart-beat:10000,10000

09:18:06 STOMP Connection closed to wss://example.com/websocket
09:18:06 WS onWebSocketClose {"isTrusted":false,"code":1002,"reason":""}
09:18:06 STOMP STOMP: scheduling reconnection in 5000ms
09:18:11 STOMP Opening Web Socket...
09:18:21 STOMP Connection not established in 10000ms, closing socket
09:18:21 STOMP Issuing close on the websocket
09:18:21 STOMP Web Socket Opened...
09:18:21 STOMP
    >>> CONNECT
    accept-version:1.0,1.1,1.2
    heart-beat:10000,10000
// This keeps going for ever, until manual intervention.

3. 重新启动服务器应用程序后成功重新连接的客户端日志

  • 这些日志显示了当服务器关闭时客户端尝试重新连接时我们应该期待什么。
  • 服务器应用程序在07:04:11和之间重新启动07:05:15
  • 预计我们的 apache 服务器503会在重新启动时返回。
  • 当服务器应用程序返回时,客户端尝试在07:05:20. 在同一秒钟内建立了连接,服务器返回了一个CONNECTED回复。
// No previous logs in the past couple hours.
07:04:11 STOMP Connection closed to wss://example.com/websocket
07:04:11 WS onWebSocketClose {"isTrusted":false,"code":1001,"reason":""}
07:04:11 STOMP STOMP: scheduling reconnection in 5000ms
07:04:16 STOMP Opening Web Socket...
07:04:16 WS onWebSocketError {"isTrusted":false,"message":"Expected HTTP 101 response but was '503 Service Unavailable'"}
07:04:17 STOMP Connection closed to wss://example.com/websocket
07:04:17 WS onWebSocketClose {"isTrusted":false,"message":"Expected HTTP 101 response but was '503 Service Unavailable'"}
07:04:17 STOMP STOMP: scheduling reconnection in 5000ms
07:04:22 STOMP Opening Web Socket...
07:04:22 WS onWebSocketError {"isTrusted":false,"message":"Expected HTTP 101 response but was '503 Service Unavailable'"}
07:04:22 STOMP Connection closed to wss://example.com/websocket
07:04:22 WS onWebSocketClose {"isTrusted":false,"message":"Expected HTTP 101 response but was '503 Service Unavailable'"}
07:04:22 STOMP STOMP: scheduling reconnection in 5000ms
07:04:27 STOMP Opening Web Socket...
07:04:27 WS onWebSocketError {"isTrusted":false,"message":"Expected HTTP 101 response but was '503 Service Unavailable'"}
07:04:27 STOMP Connection closed to wss://example.com/websocket
07:04:27 WS onWebSocketClose {"isTrusted":false,"message":"Expected HTTP 101 response but was '503 Service Unavailable'"}
07:04:27 STOMP STOMP: scheduling reconnection in 5000ms
07:04:32 STOMP Opening Web Socket...
07:04:32 WS onWebSocketError {"isTrusted":false,"message":"Expected HTTP 101 response but was '503 Service Unavailable'"}
07:04:32 STOMP Connection closed to wss://example.com/websocket
07:04:32 WS onWebSocketClose {"isTrusted":false,"message":"Expected HTTP 101 response but was '503 Service Unavailable'"}
07:04:32 STOMP STOMP: scheduling reconnection in 5000ms
07:04:37 STOMP Opening Web Socket...
07:04:38 WS onWebSocketError {"isTrusted":false,"message":"Expected HTTP 101 response but was '503 Service Unavailable'"}
07:04:38 STOMP Connection closed to wss://example.com/websocket
07:04:38 WS onWebSocketClose {"isTrusted":false,"message":"Expected HTTP 101 response but was '503 Service Unavailable'"}
07:04:38 STOMP STOMP: scheduling reconnection in 5000ms
07:04:43 STOMP Opening Web Socket...
07:04:43 WS onWebSocketError {"isTrusted":false,"message":"Expected HTTP 101 response but was '503 Service Unavailable'"}
07:04:43 STOMP Connection closed to wss://example.com/websocket
07:04:43 WS onWebSocketClose {"isTrusted":false,"message":"Expected HTTP 101 response but was '503 Service Unavailable'"}
07:04:43 STOMP STOMP: scheduling reconnection in 5000ms
07:04:48 STOMP Opening Web Socket...
07:04:48 WS onWebSocketError {"isTrusted":false,"message":"Expected HTTP 101 response but was '503 Service Unavailable'"}
07:04:48 STOMP Connection closed to wss://example.com/websocket
07:04:48 WS onWebSocketClose {"isTrusted":false,"message":"Expected HTTP 101 response but was '503 Service Unavailable'"}
07:04:48 STOMP STOMP: scheduling reconnection in 5000ms
07:04:53 STOMP Opening Web Socket...
07:04:54 WS onWebSocketError {"isTrusted":false,"message":"Expected HTTP 101 response but was '503 Service Unavailable'"}
07:04:54 STOMP Connection closed to wss://example.com/websocket
07:04:54 WS onWebSocketClose {"isTrusted":false,"message":"Expected HTTP 101 response but was '503 Service Unavailable'"}
07:04:54 STOMP STOMP: scheduling reconnection in 5000ms
07:04:59 STOMP Opening Web Socket...
07:04:59 WS onWebSocketError {"isTrusted":false,"message":"Expected HTTP 101 response but was '503 Service Unavailable'"}
07:04:59 STOMP Connection closed to wss://example.com/websocket
07:04:59 WS onWebSocketClose {"isTrusted":false,"message":"Expected HTTP 101 response but was '503 Service Unavailable'"}
07:04:59 STOMP STOMP: scheduling reconnection in 5000ms
07:05:04 STOMP Opening Web Socket...
07:05:04 WS onWebSocketError {"isTrusted":false,"message":"Expected HTTP 101 response but was '503 Service Unavailable'"}
07:05:04 STOMP Connection closed to wss://example.com/websocket
07:05:04 WS onWebSocketClose {"isTrusted":false,"message":"Expected HTTP 101 response but was '503 Service Unavailable'"}
07:05:04 STOMP STOMP: scheduling reconnection in 5000ms
07:05:09 STOMP Opening Web Socket...
07:05:09 WS onWebSocketError {"isTrusted":false,"message":"Expected HTTP 101 response but was '503 Service Unavailable'"}
07:05:09 STOMP Connection closed to wss://example.com/websocket
07:05:09 WS onWebSocketClose {"isTrusted":false,"message":"Expected HTTP 101 response but was '503 Service Unavailable'"}
07:05:09 STOMP STOMP: scheduling reconnection in 5000ms
07:05:14 STOMP Opening Web Socket...
07:05:15 WS onWebSocketError {"isTrusted":false,"message":"Expected HTTP 101 response but was '503 Service Unavailable'"}
07:05:15 STOMP Connection closed to wss://example.com/websocket
07:05:15 WS onWebSocketClose {"isTrusted":false,"message":"Expected HTTP 101 response but was '503 Service Unavailable'"}
07:05:15 STOMP STOMP: scheduling reconnection in 5000ms
07:05:20 STOMP Opening Web Socket...
07:05:20 STOMP Web Socket Opened...
07:05:20 STOMP
    >>> CONNECT
    accept-version:1.0,1.1,1.2
    heart-beat:10000,10000

07:05:20 STOMP
    <<< CONNECTED
    heart-beat:10000,10000
    version:1.2
    content-length:0

07:05:20 STOMP connected to server undefined 
07:05:20 TRACE ws.service.ts: stompSocket.onConnect() was called
// ...
4

0 回答 0