Broadcast for all connected clients except sender with python socketio flash drive

I am following the Alex Hadik Flask Socketio tutorial, which talks about a simple bulb chat application

http://www.alexhadik.com/blog/2015/1/29/using-socketio-with-python-and-flask-on-heroku

I would like to pass the message to all connected users except the sender. I passed the flash drive using init .py, but I'm still not sure how to do this.

Here is the server code.

from flask import Flask, render_template,request
from flask.ext.socketio import SocketIO,emit,send
import json,sys

app = Flask(__name__)
socketio = SocketIO(app)
clients = {}
@app.route("/")
def index():
    return render_template('index.html',)

@socketio.on('send_message')
def handle_source(json_data):
    text = json_data['message'].encode('ascii', 'ignore')
    current_client = request.namespace
    current_client_id = request.namespace.socket.sessid
    update_client_list(current_client,current_client_id)
    if clients.keys():
        for client in clients.keys():
            if not current_client_id in client:
                clients[client].socketio.emit('echo', {'echo': 'Server Says: '+text})       

def update_client_list(current_client,current_client_id):
    if not current_client_id in clients: clients[current_client_id] = current_client
    return 

if __name__ == "__main__":
    socketio.run(app,debug = False)

. dict (), request.name, . [client].socketio.emit() , , - , .

- , , ?

+5
4

, emit('my_event', {data:my_data}, broadcast=True, include_self=False). :

@socketio.on('send_message')
    def handle_source(json_data):
        text = json_data['message'].encode('ascii', 'ignore')
        emit('echo', {'echo': 'Server Says: '+text}, broadcast=True, include_self=False)

, rooms, emit('my_event', {data:my_data}, room=my_room, include_self=False) , my_room. flask_socketio.emit .

+3

@Alex, , , Python:

emit('echo', {'data':'what ever you are trying to send'},  broadcast=True)
+1

socket.broadcast.emit socket.emit. , Python, , Socket.io Node.js.

0

https://flask-socketio.readthedocs.io/en/latest/#flask_socketio.SocketIO.emit

skip_sid socketio.emit().

skip_sid - identifier of the client session that should be ignored when broadcasting or addressing the room. As a rule, it is installed on the sender of the message, so that everyone except this client receives the message. To skip multiple sides, pass a list.

0
source

All Articles