-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
63 lines (50 loc) · 1.26 KB
/
main.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import time, threading
from flask import Flask, render_template
from flask.ext.socketio import SocketIO
from lib import webchat_lib
app = Flask(__name__)
socketio = SocketIO(app)
redis_conn = webchat_lib.conn_redis_pubsub()
sub_chat = redis_conn.pubsub()
sub_member = redis_conn.pubsub()
sub_chat.subscribe('chat')
sub_member.subscribe('member')
def listen_chat():
for m in sub_chat.listen():
if m['data'] :
socketio.send(str(m['data']))
try :
t = threading.Thread(target=listen_chat)
t.deamon = True
t.start()
except (KeyboardInterrupt, SystemExit):
raise
except:
print "KeyboardInterrupted!! exit!"
socketio.server.stop()
@app.route('/')
def index():
nick = "guest_" + str(time.time()).split(".")[0]
return render_template("main.html", nick_name=nick)
@socketio.on('message')
def handle_message(msg):
global redis_conn
new_msg = webchat_lib.trim_msg(msg)
redis_conn.publish('chat', new_msg)
print new_msg
return
@socketio.on('connect')
def handle_connect(nick):
new_nick = webchat_lib.trim_msg(nick)
socketio.emit('connect', new_nick)
return
@socketio.on('disconnect')
def handle_disconnect():
print "disconnected"
return
if __name__ == '__main__':
app.debug = True
socketio.run(app)
# app.run('0.0.0.0')