-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
86 lines (64 loc) · 2.14 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import socketio
import eventlet
from flask import Flask
import redis
import urllib.request, urllib.error, urllib.parse
import json
from urllib.error import HTTPError
from datetime import datetime
# from request import getdata
sio = socketio.Server()
app = Flask(__name__)
r = redis.StrictRedis(host='localhost', port=6379, db=0)
def getdata(question_id, api_key):
# request to rest api
url = 'http://127.0.0.1:8000/v1/questions/'+str(question_id)+'/SimpleResult'
req = urllib.request.Request(url)
req.add_header('api-key', api_key)
try:
response_json = urllib.request.urlopen(req).read()
response_json = json.loads(response_json.decode('utf-8'))
except HTTPError:
print(HTTPError.reason)
return {'error': 'true', 'desc': 'Socket server failed to get data from api server.'}
print(response_json)
return response_json
@app.route('/')
def index():
return 'Hello World!'
@sio.on('connect')
def connect(sid, environ):
print('connect ', sid)
r.set(sid, datetime.now())
@sio.on('send')
def message(sid, data):
print('message ', data)
# enter question_id room
sio.enter_room(sid, data['question_id'])
r.sadd(data['question_id'], sid)
# rooms
r.sadd('questions', data['question_id'])
# get data from this questin_id
response = getdata(data['question_id'], data['api-key'])
# return full response data to send client
sio.emit('reply', response, room=sid)
# return partial response data to room
sio.emit('reply', data, room=data['question_id'], skip_sid=sid)
r.set(sid, datetime.now())
@sio.on('disconnect')
def disconnect(sid):
print('disconnect ', sid)
# Delete client time info
r.delete(sid)
# Delete client question info
for s in r.smembers('questions'):
r.srem(s, sid)
if not r.smembers(s):
print(s,"is empty")
r.srem('questions', s)
# r.delete(s)
if __name__ == '__main__':
# wrap Flask application with socketio's middleware
app = socketio.Middleware(sio, app)
# deploy as an eventlet WSGI server
eventlet.wsgi.server(eventlet.listen(('127.0.0.1', 5000)), app)