简体   繁体   中英

get current connection in flask socket.io

I want to make multiple emitting to an individual socket connection using flask's socket.io extension.

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?")

I do not know which connection saying2 is emitting to. Should I pass current connection to saying2 method? How can I achieve my goal?

In your example, saying2() is emitting to the client that sent the /saySomething event. This is based on a concept similar to request contexts in standard Flask.

The emit function has two optional arguments to send to other clients:

  • broadcast=True will send to all connected clients, including the client that sent the /saySomething event.
  • room=<room-name> will send to all clients attached to the given room. If you want to address individual clients, then put each client in a different room and target the desired room.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM