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

如何检测客户端掉线 #11

Closed
soitun opened this issue Nov 30, 2020 · 11 comments
Closed

如何检测客户端掉线 #11

soitun opened this issue Nov 30, 2020 · 11 comments

Comments

@soitun
Copy link

soitun commented Nov 30, 2020

我现在是通过扩展服务器的 /term 事件来做客户端掉线检测,然后更新数据库的客户端在线状态
在客户端设置了keeplive , 发现不管设置keeplive 为多少,客户端还是会经常离线。
请问有什么更好的方案来检测,谢谢

@jacoblai
Copy link
Contributor

jacoblai commented Nov 30, 2020 via email

@jacoblai
Copy link
Contributor

jacoblai commented Dec 1, 2020

请把你在使用的客户端库告诉我,我去复现一下。

@soitun
Copy link
Author

soitun commented Dec 2, 2020

我用的是
github.com/eclipse/paho.mqtt.golang
谢谢

@jacoblai
Copy link
Contributor

jacoblai commented Dec 2, 2020 via email

@soitun
Copy link
Author

soitun commented Dec 2, 2020

是的,没有特殊的环境的

@jacoblai
Copy link
Contributor

jacoblai commented Dec 2, 2020 via email

@jacoblai
Copy link
Contributor

jacoblai commented Dec 2, 2020

请参考这个订阅示例:https://github.com/Coolpy7/coolpy7_benchmark/blob/master/clients_samples/paho_golang_sub_client.go
这里有两点需要说明:1.SetDefaultPublishHandler里的处理代码必须没有阻塞,需要现在这样的代码去处理接收处理逻辑。2.如果网络环境极为不稳定,可以把我注释掉的自动重连功能开启,再把clear session设置为false.

@soitun
Copy link
Author

soitun commented Dec 2, 2020

谢谢,马上试下

@soitun
Copy link
Author

soitun commented Dec 4, 2020

按上面的例子已经理好,谢谢

@soitun soitun closed this as completed Dec 4, 2020
@jacoblai
Copy link
Contributor

jacoblai commented Dec 4, 2020 via email

@jacoblai
Copy link
Contributor

jacoblai commented Dec 12, 2020

还有一个问题需要注意,不然的话很可能会把系统的cpu用尽,原因是for goruntine时没有没有做时间中断,所以cpu会一直占用很高,解决办法是增加for的time.sleep,代码如下:

for {
		select {
		case incoming := <-d.Choke:
			//d.mqttHandler(incoming.Client, incoming.Message)
		case <-d.Quit:
			return
		default:
			time.Sleep(20*time.Millisecond) //必须增加此句代码让for行业适当的中断时间,cpu才能合理释放。
		}
	}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants