流式聊天:websocket 已断开连接,聊天列表不会在重新连接时更新
Stream chat: websocket disconnected, chat list not updating on reconnect
正在试验 Stream's chat service, and we're considering to migrate to it for our iOS app. I'm using version 2.6.2 of their SDK for iOS。但是,当 websocket 断开连接时,我有时会收到此错误:
图片是挪威语,但英文是:The action couldn't be completed (STREAMCHATCLIENT.WEBSOCKETPROVIDERERROR-bug 1)
。
ChannelsViewController
中的聊天列表作为断开连接后清理状态的一部分被清除,如日志中所示:
[14 Jan 09:36:55.487] [DEBUG] Clearing state after disconnect...
[14 Jan 09:36:55.487] [DEBUG] Cancelling background work...
[14 Jan 09:36:55.489] [DEBUG] :purple_heart: Background mode off
[14 Jan 09:36:55.489] [DEBUG] Disconnecting: Processing finished in background
[14 Jan 09:36:55.491] [DEBUG] Parsing WebSocket disconnect... (error: Cancelled)
[14 Jan 09:36:55.492] [DEBUG] Clearing state after disconnect...
[14 Jan 09:36:55.492] [DEBUG] Cancelling background work...
[14 Jan 09:36:55.492] [ERROR] :x: :broken_heart::rage: Disconnected by error WebSocketProviderError(reason: "Cancelled", code: -1, providerType: StreamChatClient.StarscreamWebSocketProvider, providerError: nil) in websocketDidDisconnect(error:)[304]
问题是即使 websocket 很快重新连接,UI 中的聊天列表也永远不会更新。
如何防止在 UI 中随机弹出此类错误,以及如何确保在 websocket 重新连接后更新 UI 中的聊天列表?
似乎多次设置用户 Client.shared.set(user:token)
可能会导致问题。确保您没有设置已经设置的用户。错误仍然发生几次,但不再那么严重了。我添加了这张支票:
guard Client.shared.user.role == .anonymous else {
log("User is already logged in. Aborting chat login process")
return
}
请注意,如果有人在注销后登录并且用户在注销时未正确重置,则此检查将失败。在文档中它说要注销您必须 运行 Client.shared.disconnect()
,但您还必须使用 Client.shared.setAnonymousUser()
重置用户。之前用户的 ID 和所有内容也将被删除。
正在试验 Stream's chat service, and we're considering to migrate to it for our iOS app. I'm using version 2.6.2 of their SDK for iOS。但是,当 websocket 断开连接时,我有时会收到此错误:
The action couldn't be completed (STREAMCHATCLIENT.WEBSOCKETPROVIDERERROR-bug 1)
。
ChannelsViewController
中的聊天列表作为断开连接后清理状态的一部分被清除,如日志中所示:
[14 Jan 09:36:55.487] [DEBUG] Clearing state after disconnect...
[14 Jan 09:36:55.487] [DEBUG] Cancelling background work...
[14 Jan 09:36:55.489] [DEBUG] :purple_heart: Background mode off
[14 Jan 09:36:55.489] [DEBUG] Disconnecting: Processing finished in background
[14 Jan 09:36:55.491] [DEBUG] Parsing WebSocket disconnect... (error: Cancelled)
[14 Jan 09:36:55.492] [DEBUG] Clearing state after disconnect...
[14 Jan 09:36:55.492] [DEBUG] Cancelling background work...
[14 Jan 09:36:55.492] [ERROR] :x: :broken_heart::rage: Disconnected by error WebSocketProviderError(reason: "Cancelled", code: -1, providerType: StreamChatClient.StarscreamWebSocketProvider, providerError: nil) in websocketDidDisconnect(error:)[304]
问题是即使 websocket 很快重新连接,UI 中的聊天列表也永远不会更新。
如何防止在 UI 中随机弹出此类错误,以及如何确保在 websocket 重新连接后更新 UI 中的聊天列表?
似乎多次设置用户 Client.shared.set(user:token)
可能会导致问题。确保您没有设置已经设置的用户。错误仍然发生几次,但不再那么严重了。我添加了这张支票:
guard Client.shared.user.role == .anonymous else {
log("User is already logged in. Aborting chat login process")
return
}
请注意,如果有人在注销后登录并且用户在注销时未正确重置,则此检查将失败。在文档中它说要注销您必须 运行 Client.shared.disconnect()
,但您还必须使用 Client.shared.setAnonymousUser()
重置用户。之前用户的 ID 和所有内容也将被删除。