diff --git a/jarvis/utils/templates/index.html b/jarvis/utils/templates/index.html index 96ff693..df24580 100644 --- a/jarvis/utils/templates/index.html +++ b/jarvis/utils/templates/index.html @@ -1,28 +1,84 @@ - Socket-Test - - + Flask-SocketIO Test + + - - -

Socket

+ +

Flask-SocketIO Test

+

+ Async mode is: {{ async_mode }}
+ Current transport is:
+ Average ping/pong latency: ms +

+

Send:

- + +
+
+ + +
+
+ + +
+
+ + +
+
+ + + +
+
+ +
-
- +
-

Logs

-
+

Receive:

+
\ No newline at end of file diff --git a/jarvis/utils/websocketstest.py b/jarvis/utils/websocketstest.py index ec8540f..ca37e50 100644 --- a/jarvis/utils/websocketstest.py +++ b/jarvis/utils/websocketstest.py @@ -1,39 +1,110 @@ -from flask import Flask, render_template, session, copy_current_request_context -from flask_socketio import SocketIO, emit, disconnect from threading import Lock +from flask import Flask, render_template, session, request, \ + copy_current_request_context +from flask_socketio import SocketIO, emit, join_room, leave_room, \ + close_room, rooms, disconnect +# Set this variable to "threading", "eventlet" or "gevent" to test the +# different async modes, or leave it set to None for the application to choose +# the best option based on installed packages. async_mode = None + app = Flask(__name__) app.config['SECRET_KEY'] = 'secret!' -socket_ = SocketIO(app, async_mode=async_mode) +socketio = SocketIO(app, async_mode=async_mode) thread = None thread_lock = Lock() @app.route('/') def index(): - return render_template('index.html', async_mode=socket_.async_mode) + return render_template('index.html', async_mode=socketio.async_mode) -@socket_.on('my_event', namespace='/test') -def test_message(message): +@app.route('/test') +def test(): + send_text_to_room("salut c'est serveur", "192.168.1.130") + return "OK" + + +def send_text_to_room(text, room_id): + session['receive_count'] = session.get('receive_count', 0) + 1 + socketio.emit('my_response', {'data': text, 'count': session['receive_count']}, to=room_id) + + +@socketio.event +def my_event(message): session['receive_count'] = session.get('receive_count', 0) + 1 emit('my_response', {'data': message['data'], 'count': session['receive_count']}) -@socket_.on('disconnect_request', namespace='/test') +@socketio.event +def my_broadcast_event(message): + session['receive_count'] = session.get('receive_count', 0) + 1 + emit('my_response', + {'data': message['data'], 'count': session['receive_count']}, + broadcast=True) + + +@socketio.event +def join(message): + join_room(message['room']) + session['receive_count'] = session.get('receive_count', 0) + 1 + emit('my_response', + {'data': 'In rooms: ' + ', '.join(rooms()), + 'count': session['receive_count']}) + + +@socketio.event +def leave(message): + leave_room(message['room']) + session['receive_count'] = session.get('receive_count', 0) + 1 + emit('my_response', + {'data': 'In rooms: ' + ', '.join(rooms()), + 'count': session['receive_count']}) + + +@socketio.on('close_room') +def on_close_room(message): + session['receive_count'] = session.get('receive_count', 0) + 1 + emit('my_response', {'data': 'Room ' + message['room'] + ' is closing.', + 'count': session['receive_count']}, + to=message['room']) + close_room(message['room']) + + +@socketio.event def disconnect_request(): @copy_current_request_context def can_disconnect(): disconnect() session['receive_count'] = session.get('receive_count', 0) + 1 + # for this emit we use a callback function + # when the callback function is invoked we know that the message has been + # received and it is safe to disconnect emit('my_response', {'data': 'Disconnected!', 'count': session['receive_count']}, callback=can_disconnect) +@socketio.event +def my_ping(): + emit('my_pong') + + +@socketio.event +def connect(): + global thread + emit('my_response', {'data': 'Connected', 'count': 0}) + + +@socketio.on('disconnect') +def test_disconnect(): + print('Client disconnected', request.sid) + + if __name__ == '__main__': - socket_.run(app, debug=True, host='0.0.0.0') + socketio.run(app)