Reputation: 1
I'm testing the Flask-SocketIO package. My config is:
Server: Synology NAS DS218J (unix) on local network, running the basic example over Python 3.9.14.
Python script running on server:
from flask import Flask, render_template
from flask_socketio import SocketIO, emit
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app, async_mode=None, cors_allowed_origins="*",
ping_interval=60, ping_timeout=60,
logger=True, engineio_logger=True)
@app.route('/')
def index():
return render_template('index.html')
@socketio.event
def my_event(message):
emit('my_response', {'data': message + ': got it!'})
@socketio.event
def my_ping():
emit('my_pong')
if __name__ == '__main__':
socketio.run(app, port=8080)
index.html send to client browser:
<!DOCTYPE HTML>
<html>
<head>
<title>Flask-SocketIO Test</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"
integrity="sha512-bLT0Qm9VnAYZDflyKcBaQ2gg0hSYNQrJ8RilYldYQ1FxQYoCLtUjuuRuZo+fjqhx/qtq/1itJ0C2ejDxltZVFg=="
crossorigin="anonymous"></script>
<script src="https://cdn.socket.io/4.7.5/socket.io.min.js"
integrity="sha384-2huaZvOR9iDzHqslqwpR87isEmrfxqyWOF7hr7BY6KG0+hVKLoEXMPUJw3ynWuhO"
crossorigin="anonymous"></script>
<script type="text/javascript" charset="utf-8">
$(document).ready(function () {
// Connect to the Socket.IO server.
// The connection URL has the following format, relative to the current page:
// http[s]://<domain>:<port>[/<namespace>]
// var socket = io();
var socket = io('http://192.168.0.20:8080');
// Event handler for new connections.
// The callback function is invoked when a connection with the
// server is established.
socket.on('connect', function () {
socket.emit('my_event', { data: 'I\'m connected!' });
});
// Event handler for server sent data.
// The callback function is invoked whenever the server emits data
// to the client. The data is then displayed in the "Received"
// section of the page.
socket.on('my_response', function (msg, cb) {
$('#log').append('<br>' + $('<div/>').text('Received #' + msg.count + ': ' + msg.data).html());
if (cb)
cb();
});
// Interval function that tests message latency by sending a "ping"
// message. The server then responds with a "pong" message and the
// round trip time is measured.
var ping_pong_times = [];
var start_time;
window.setInterval(function () {
start_time = (new Date).getTime();
$('#transport').text(socket.io.engine.transport.name);
socket.emit('my_ping');
}, 1000);
// Handler for the "pong" message. When the pong is received, the
// time from the ping is stored, and the average of the last 30
// samples is average and displayed.
socket.on('my_pong', function () {
var latency = (new Date).getTime() - start_time;
ping_pong_times.push(latency);
ping_pong_times = ping_pong_times.slice(-30); // keep last 30 samples
var sum = 0;
for (var i = 0; i < ping_pong_times.length; i++)
sum += ping_pong_times[i];
$('#ping-pong').text(Math.round(10 * sum / ping_pong_times.length) / 10);
});
// Handlers for the different forms in the page.
// These accept data from the user and send it to the server in a
// variety of ways
$('form#emit').submit(function (event) {
socket.emit('my_event', { data: $('#emit_data').val() });
return false;
});
$('form#broadcast').submit(function (event) {
socket.emit('my_broadcast_event', { data: $('#broadcast_data').val() });
return false;
});
$('form#join').submit(function (event) {
socket.emit('join', { room: $('#join_room').val() });
return false;
});
$('form#leave').submit(function (event) {
socket.emit('leave', { room: $('#leave_room').val() });
return false;
});
$('form#send_room').submit(function (event) {
socket.emit('my_room_event', { room: $('#room_name').val(), data: $('#room_data').val() });
return false;
});
$('form#close').submit(function (event) {
socket.emit('close_room', { room: $('#close_room').val() });
return false;
});
$('form#disconnect').submit(function (event) {
socket.emit('disconnect_request');
return false;
});
});
</script>
</head>
<body>
<h1>Flask-SocketIO Test</h1>
<p>
Async mode is: <b>{{ async_mode }}</b><br>
Current transport is: <b><span id="transport"></span></b><br>
Average ping/pong latency: <b><span id="ping-pong"></span>ms</b>
</p>
<h2>Send:</h2>
<form id="emit" method="POST" action='#'>
<input type="text" name="emit_data" id="emit_data" placeholder="Message">
<input type="submit" value="Echo">
</form>
<form id="broadcast" method="POST" action='#'>
<input type="text" name="broadcast_data" id="broadcast_data" placeholder="Message">
<input type="submit" value="Broadcast">
</form>
<form id="join" method="POST" action='#'>
<input type="text" name="join_room" id="join_room" placeholder="Room Name">
<input type="submit" value="Join Room">
</form>
<form id="leave" method="POST" action='#'>
<input type="text" name="leave_room" id="leave_room" placeholder="Room Name">
<input type="submit" value="Leave Room">
</form>
<form id="send_room" method="POST" action='#'>
<input type="text" name="room_name" id="room_name" placeholder="Room Name">
<input type="text" name="room_data" id="room_data" placeholder="Message">
<input type="submit" value="Send to Room">
</form>
<form id="close" method="POST" action="#">
<input type="text" name="close_room" id="close_room" placeholder="Room Name">
<input type="submit" value="Close Room">
</form>
<form id="disconnect" method="POST" action="#">
<input type="submit" value="Disconnect">
</form>
<h2>Receive:</h2>
<div id="log"></div>
</body>
</html>
Client: Google Chrome webbrowser from single Windows-PC on same local network.
When I connect to the server, this returns the index.html
as expected (so the server script is doing it right), but no further responses from server are received, even if I use the interface buttons of the webpage.
The Python error log shows this errors continuously:
2024-11-13T11:09:48+01:00 DS218J python3.9-uwsgi[9810]: d4PXlWixzGEEkGSDAAAA: Sending packet OPEN data {'sid': 'd4PXlWixzGEEkGSDAAAA', 'upgrades': ['websocket'], 'pingTimeout': 60000, 'pingInterval': 60000}
2024-11-13T11:09:48+01:00 DS218J python3.9-uwsgi[9810]: [pid: 9999|app: 0|req: 2/2] 192.168.0.163 () {42 vars in 744 bytes} [Wed Nov 13 11:09:48 2024] GET /socket.io/?EIO=4&transport=polling&t=PCb01mX => generated 97 bytes in 4 msecs (HTTP/1.1 200) 3 headers in 149 bytes (2 switches on core 1)
2024-11-13T11:09:48+01:00 DS218J python3.9-uwsgi[9810]: Invalid session d4PXlWixzGEEkGSDAAAA (further occurrences of this error will be logged with level INFO)
2024-11-13T11:09:48+01:00 DS218J python3.9-uwsgi[9810]: Invalid session d4PXlWixzGEEkGSDAAAA (further occurrences of this error will be logged with level INFO)
2024-11-13T11:09:48+01:00 DS218J python3.9-uwsgi[9810]: [pid: 10000|app: 0|req: 1/3] 192.168.0.163 () {46 vars in 889 bytes} [Wed Nov 13 11:09:48 2024] POST /socket.io/?EIO=4&transport=polling&t=PCb01mj&sid=d4PXlWixzGEEkGSDAAAA => generated 17 bytes in 7 msecs (HTTP/1.1 400) 3 headers in 143 bytes (1 switches on core 0)
2024-11-13T11:09:48+01:00 DS218J python3.9-uwsgi[9810]: Invalid session d4PXlWixzGEEkGSDAAAA
2024-11-13T11:09:48+01:00 DS218J python3.9-uwsgi[9810]: [pid: 10004|app: 0|req: 2/4] 192.168.0.163 () {42 vars in 794 bytes} [Wed Nov 13 11:09:48 2024] GET /socket.io/?EIO=4&transport=polling&t=PCb01mk&sid=d4PXlWixzGEEkGSDAAAA => generated 17 bytes in 1 msecs (HTTP/1.1 400) 3 headers in 143 bytes (1 switches on core 1)
I've read other posts related with the same problem, but none solved the problem. I've also tryed executing the script on the single windows PC (127.0.0.1), and all worked OK, as expected. The problem is that I need the python script being executed on the server... So please, can someone help on this? Thanks in advance.
Upvotes: 0
Views: 46