/
server.py
53 lines (38 loc) · 1.25 KB
/
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from flask import Flask, current_app
from flask import jsonify, redirect, url_for, escape
from flask import request, session
from flask import g as Globals
from flask.ext.socketio import SocketIO, emit, join_room, leave_room
import logging
logging.basicConfig()
app = Flask(__name__)
#app.config['SECRET_KEY'] = 'secret!'
app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT'
socketio = SocketIO(app)
app.artist = 0
app.log = {}
@app.route('/<path:path>')
def static_proxy(path):
return app.send_static_file(path)
@app.route('/draw/<room>')
def drawing_room(room):
session['artist'] = str(app.artist)
session['room'] = room
app.artist += 1
return app.send_static_file('draw.html')
@socketio.on('connect', namespace='/draw_data')
def connect():
join_room(session['room'])
if session['room'] not in app.log:
app.log[session['room']] = []
for move in app.log[session['room']]:
emit('server_to_client_move', move, room=session['room'])
@socketio.on('client_to_server_move', namespace='/draw_data')
def received_move(obj):
move = {'point' : obj['point'],
'isNew': obj['isNew'],
'artist':session['artist']}
app.log[session['room']].append(move)
emit('server_to_client_move', move, room=session['room'])
if __name__ == '__main__':
socketio.run(app)