I'v created a Flask-SocketIO python server that should log data from a socket.io JavaScript client.
Server:
from flask import Flask, render_template
from flask_socketio import SocketIO
app = Flask(__name__)
socketio = SocketIO(app)
@socketio.on('connect')
def handle_connect():
print 'Connected'
@socketio.on('disconnect')
def handle_disconnect():
print 'Disconnected'
@socketio.on('start')
def handle_start():
print 'Start'
@socketio.on('log')
def handle_log(data):
print('Log: ' + data)
if __name__ == '__main__':
socketio.run(app)
Client:
<script src="socket.io.js"></script>
<script>
function sleep(ms) {
var unixtime_ms = new Date().getTime();
while(new Date().getTime() < unixtime_ms + ms) {}
}
var socket = io.connect('http://127.0.0.1:5000', {'forceNew':true });
socket.on('connect', function()
{
socket.emit('start');
socket.emit('log', '1');
socket.emit('log', '2');
socket.emit('log', '3');
sleep(3000)
});
</script>
Instead of seeing the server printing "Start", "Log: 1", "Log: 2" and "Log: 3" immediately.
The server only prints "Start" and after 3 seconds prints all the other messages in one shot.
This is a problem as I require realtime logging, and cannot afford receiving all the logs in one shot at the end of execution.
Any idea why this is happening and what is the correct way to implement this simple log server?
sleep(3000)call?sleep(). You can't block, as JS is an asynchronous language.