-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
33 lines (27 loc) · 835 Bytes
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from flask import Flask, render_template, sessions, request
import requests
from flask_socketio import SocketIO, emit
app = Flask(__name__)
app.secret_key = "dangvansam"
socketio = SocketIO(app)
count = 0
@app.route('/')
def index():
#print(requests.sid)
return render_template("client.html")
@socketio.on('connect')
def connect():
global count
count += 1
emit('count_client', {'count': count}, broadcast=True)
@socketio.on('disconnect')
def disconnect():
global count
count -= 1
emit('count_client', {'count': count}, broadcast=True)
@socketio.on('client_to_server')
def handle_client_request(data):
#print(requests.sid)
socketio.emit('server_to_client', {'username':data['username'], 'text': data['text']}, broadcast=True)
if __name__ == "__main__":
socketio.run(app, debug=True)