-
Notifications
You must be signed in to change notification settings - Fork 72
/
chat.py
executable file
·53 lines (41 loc) · 1.19 KB
/
chat.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
#!/usr/bin/env python3
# Standard Library
from datetime import datetime
# Thirdparty Library
import eventlet
import requests
import socketio
sio = socketio.Server(cors_allowed_origins='*')
app = socketio.WSGIApp(sio)
@sio.event
def connect(sid, environ):
print('connect ', sid)
@sio.event
def disconnect(sid):
print('disconnect ', sid)
@sio.on('chat')
def message_chat(sid, message):
# Registramos el mensaje
print('\n\nentrando\n\n')
res = requests.post('http://0.0.0.0:5000/chat/register_message', data={
'sid': sid,
'message': message,
'date': datetime.now().strftime('%Y-%m-%d %H:%M:%S')
})
print('\n\n%s\n\n' % res.text)
# Luego lo emitimos
sio.emit('master', {
'sid': sid,
'message': message,
})
@sio.on('chatr')
def message_response(sid, data):
requests.post('http://0.0.0.0:5000/chat/register_message', data={
'sid': data['sid'],
'message': data['message'],
'date': datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
'response': True
})
sio.emit('chat-%s' % data['sid'], data['message'])
if __name__ == '__main__':
eventlet.wsgi.server(eventlet.listen(('0.0.0.0', 5000)), app)