我有以下线程发送到正在工作的websocket,但如果我有两个浏览器窗口(或多个客户端连接),他们都会看到相同的数据被推送。在https://github.com/miguelgrinberg/Flask-SocketIO的示例代码中,我甚至不需要指定broadcast = False,但我尝试了它,它仍然显示在所有连接的websocket窗口中。
@socketio.on('job_submit', namespace='/socket')
def job_submit(message):
emitter('received job')
# start job and kick off thread to read and emit output of job (setup in redis list)
job = background_task.delay()
runme = test_duplicates(message)
if runme:
threads[len(threads) + 1] = {'thread': None, 'thread_lock': Lock()}
global thread
thread = threads[len(threads)]['thread']
with threads[len(threads)]['thread_lock']:
if thread is None:
thread = socketio.start_background_task(output_puller, job)
threads[len(threads)]['thread'] = thread
threads[len(threads)]['thread'].setName(message['data'])
def output_puller(job):
while job.state != 'SUCCESS' and job.state != 'FAILURE':
result = r_server.lpop(job.id)
if result:
socketio.emit('my_response', {'data': result.decode()}, namespace='/socket', broadcast=False)
print(result)
答案 0 :(得分:1)
我的默认设置,socket.io将广播给每个连接的人。如果要广播到特定客户端,则需要在connect:
上获取会话ID下面是一个烧瓶示例
@io.on('connected')
def connected():
print "%s connected" % (request.namespace.socket.sessid)
clients.append(request.namespace)
当你想发送时。
clients[k].emit('message', "Hello at %s" % (datetime.now()))
告诉它要发送到哪个客户端而不是全局发送