In the supplied source code,
The client-side has two event-handlers:
connect
andserver_to_client
. When the page loads for the first time, theconnect
emits texts:hello!
andworld!
.Then, on the server side, the function
server_to_client()
received this message, prints it on the console, and subsequently emits another messagereceived from the server
to the client.Finally, on the client side, the event-handler
server_to_client
prints the server side message to an H2-tag.
As you can see, two functions are working at the client side, and only one function is working at the server side.
On the server side, the same function is handling client_to_server
event and raising server_to_client
event.
Say, I want to use two different functions on the server side. I.e. one function will print hello! world!
, and a different function will emit received from the server
to the client.
Is it possible?
server_sends_client_receives.html
<!DOCTYPE HTML>
<html>
<head>
<title>Long task</title>
<script src="https://code.jquery.com/jquery-3.6.3.js"></script>
<script src="https://cdn.socket.io/4.5.4/socket.io.min.js"></script>
<script type="text/javascript" charset="utf-8">
$(document).on('click', '.widget input', function (event) {
namespace = '/test';
var socket = io(namespace);
socket.on('connect', function() {
$('#messages').append('<br/>' $('<div/>').text('Requesting task to run').html());
////myText = $("messages").text()
socket.emit('client_to_server', {'hello': 'hello!', 'world': 'world!'});
});
socket.on('server_to_client', function(msg, cb) {
$('#messages').text(msg.data);
if (cb)
cb();
});
event.preventDefault();
});
</script>
</head>
<body>
<div >
<input type="submit" value="Click me" />
</div>
<h3>Messages</h3>
<H2 id="messages" >xxx yyy zzz</H2>
</body>
</html>
server_sends_client_receives.py
from flask import Flask, render_template
from flask_socketio import SocketIO, emit, disconnect
from time import sleep
async_mode = None
app = Flask(__name__)
socketio_obj = SocketIO(app, async_mode=async_mode)
@app.route('/')
def index():
return render_template('server_sends_client_receives.html',
sync_mode=socketio_obj.async_mode)
@socketio_obj.on('client_to_server', namespace='/test')
def server_to_client(arg1):
try:
print('received from the client : ', arg1['hello'])
print('received from the client : ', arg1['world'])
emit('server_to_client', {'data': 'received from the server'})
disconnect()
except Exception as ex:
print(ex)
if __name__ == '__main__':
socketio_obj.run(app, host='0.0.0.0', port=80, debug=True)
CodePudding user response:
Remove the disconnect()
call, which terminates the connection with the client.
Define a separate handler that listens to 'client_to_server2'
event:
@socketio_obj.on('client_to_server', namespace='/test')
def server_to_client(arg1):
try:
print('received from the client : ', arg1['hello'])
print('received from the client : ', arg1['world'])
except Exception as ex:
print(ex)
@socketio_obj.on('client_to_server2', namespace='/test')
def server_to_client2():
try:
emit('server_to_client', {'data': 'received from the server'})
except Exception as ex:
print(ex)
Emit 'client_to_server2'
event from client:
socket.emit('client_to_server', {'hello': 'hello!', 'world': 'world!'});
socket.emit('client_to_server2');