我正在尝试创建一个“keepalive”websocket 线程,以便在有人连接到页面后每 10 秒向浏览器发送一次发射,但是我遇到了一个错误并且不知道如何解决它。
关于如何使这项工作的任何想法?
一旦发送“断开连接”,我将如何杀死该线程?
谢谢!
@socketio.on('connect', namespace='/endpoint')
def test_connect():
emit('my response', {'data': '<br>Client thinks i\'m connected'})
def background_thread():
"""Example of how to send server generated events to clients."""
count = 0
while True:
time.sleep(10)
count += 1
emit('my response', {'data': 'websocket is keeping alive'}, namespace='/endpoint')
global thread
if thread is None:
thread = Thread(target=background_thread)
thread.start()