I made a countdown timer and i placed emit function in the for loop. But emit doesn't send my timer data to client.
This is my python server-side code:
from flask import Flask,render_template,render_template_string
from flask_socketio import SocketIO, emit
app = Flask(__name__)
app.config['SECRET KEY'] = 'random'
socketio = SocketIO(app)
@app.route('/')
def index():
return render_template('index.html')
@socketio.on('test_timer')
def Timer(seconds=3600):
def hms(seconds): # hour minute second function
h = seconds // 3600
m = seconds % 3600 // 60
s = seconds % 3600 % 60
return '{:02d}:{:02d}:{:02d}'.format(h, m, s)
for i in range(seconds):
emit(hms(seconds-i),broadcast=True)
emit.sleep(1)
if __name__ == '__main__':
socketio.run(app,debug=True)
This is my client-side javascript code:
<!DOCTYPE html>
<html>
<body>
<script src="//cdnjs.cloudflare.com/ajax/libs/socket.io/2.2.0/socket.io.js" integrity="sha256-yr4fRk/GU1ehYJPAs8P4JlTgu0Hdsp4ZKrx8bDEDC3I=" crossorigin="anonymous"></script>
<script type="text/javascript" charset="utf-8">
var socket = io().connect('http://127.0.0.1:5000');
socket.on('test_timer', function(receiving_data) {
console.log(receiving_data);
});
</script>
</body>
</html>
Even if I run the server, console.log doesn't print any timer data in client-side.
test_timerto the server; all you're doing is setting up a reaction to that message on both sides, but you're never emitting it anywhere.