-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtremolo.py
52 lines (42 loc) · 1.68 KB
/
tremolo.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
import uwsgi
import gevent.select
import redis
class TremoloApp(object):
def send(self, msg):
uwsgi.websocket_send(msg)
def broadcast(self, msg):
self.r.publish(self.room, msg)
def __init__(self, inactivity_timeout=60, room='foobar', redis_host='127.0.0.1', redis_port=6379):
self.inactivity_timeout = inactivity_timeout
self.room = room
self.redis_host = redis_host
self.redis_port = redis_port
def setup(self, core_id):
pass
def __call__(self, environ, start_response):
uwsgi.websocket_handshake(environ['HTTP_SEC_WEBSOCKET_KEY'], environ.get('HTTP_ORIGIN', ''))
print "websockets..."
self.r = redis.StrictRedis(host=self.redis_host, port=self.redis_port, db=0)
channel = self.r.pubsub()
channel.subscribe(self.room)
websocket_fd = uwsgi.connection_fd()
redis_fd = channel.connection._sock.fileno()
core_id = environ['uwsgi.core']
self.setup(core_id)
while True:
ready = gevent.select.select([websocket_fd, redis_fd], [], [], 4.0)
if not ready[0]:
uwsgi.websocket_recv_nb()
for fd in ready[0]:
if fd == websocket_fd:
try:
msg = uwsgi.websocket_recv_nb()
except IOError:
self.end(core_id)
return ""
if msg:
self.websocket(core_id, msg)
elif fd == redis_fd:
msg = channel.parse_response()
if msg[0] == 'message':
uwsgi.websocket_send(msg[2])