Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

长连接手动断开和重连 #1337

Closed
tanchendong1992 opened this issue Jul 31, 2020 · 7 comments
Closed

长连接手动断开和重连 #1337

tanchendong1992 opened this issue Jul 31, 2020 · 7 comments
Labels
question Issues that have a question which should be addressed

Comments

@tanchendong1992
Copy link

你好
如何在 APP 进入后台时,手动断开长连接,从新打卡 APP 时再重新连接长连接?

@designatednerd
Copy link
Contributor

Via Google Translate since I don't speak Chinese:

Hello there
How to manually disconnect the long connection when the APP enters the background, and then reconnect the long connection when the APP is newly clocked in?

Are you asking about subscriptions or some other kind of long connection?

@tanchendong1992
Copy link
Author

你好.订阅消息. WebSocketTransport
WeChat67a3558698ad0ab1125ec3110a335a58
如果使用方法.我想在 APP 进入后台时,断开.在 APP重新打开的时候重新连接?
初学者请多见谅!

@designatednerd
Copy link
Contributor

Hello. Subscribe to news. WebSocketTransport

Here's Apple's documentation on handling lifecycle changes - The short answer is that you will probably want to call closeConnection on the webSocketTransport in your App Delegate's applicationWillResignActive method and restart the susbcription in applicationDidBecomeActive.

Note that names are different in the Scene delegate if you're using that, but I don't think you are since you're using a singleton rather than per-scene instances of Network.

Let me know if that helps!

@designatednerd designatednerd added the question Issues that have a question which should be addressed label Jul 31, 2020
@tanchendong1992
Copy link
Author

是的,这个地方使用的是单例.多个订阅消息在这里接受,然后进行分发处理.
需要在 UIApplicationWillResignActiveNotification 时,断开订阅连接
UIApplicationDidBecomeActiveNotification 重新连接订阅

是通过 self.webSocketTransport.closeConnection() 断开订阅么

@tanchendong1992
Copy link
Author

tanchendong1992 commented Jul 31, 2020

你好.我通过 调用 self.webSocketTransport.closeConnection() ,已经可以断开连接了. 在 UIApplicationDidBecomeActiveNotification 的时候怎么重新建立连接呢?
image

@designatednerd
Copy link
Contributor

Google translated:

Hello. I can disconnect by calling self.webSocketTransport.closeConnection(). How to re-establish the connection when UIApplicationDidBecomeActiveNotification?

Ah, because addSubscribe won't work if the web socket isn't connected, and right now there's not a way to tell the websocket to reconnect non-automatically.

The good news is #1335 will add the ability to reconnect more easily. Until then you'll probably need to hack it by using either updateHeaderValues or updateConnectingPayload to force a reconnection.

@designatednerd
Copy link
Contributor

#1335 is merged to main if you want to use it immediately, it'll come out probably sometime later this week in an official release. I'm going to close this out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Issues that have a question which should be addressed
Projects
None yet
Development

No branches or pull requests

2 participants