在Flask插座上接上电流

2024-04-26 04:52:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用flask的插座.io扩展。在

from flask.ext.socketio import SocketIO, emit, join_room, leave_room


# creating flask app and io...

@io.on("/saySomething")
def saying():
  emit("said", "hello")
  saying2()

def saying2():
  # ...
  # doing something long and important
  # ...
  emit("said", "and how are you?")

我不知道saying2是向哪个连接发射的。我应该将当前连接传递到saying2方法吗?我怎样才能实现我的目标?在


Tags: andfromioimportflaskdefext插座
1条回答
网友
1楼 · 发布于 2024-04-26 04:52:27

在您的示例中,saying2()向发送/saySomething事件的客户机发出。这是基于一个类似于标准烧瓶中的请求上下文的概念。在

emit函数有两个可选参数要发送给其他客户端:

  • broadcast=True将发送到所有连接的客户端,包括发送/saySomething事件的客户端。在
  • room=<room-name>将发送到指定房间的所有客户端。如果你想解决个别客户,那么把每个客户放在一个不同的房间,并瞄准所需的房间。在

相关问题 更多 >