-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
65 lines (50 loc) · 1.59 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
import asyncio
import websockets
from http.server import HTTPServer, SimpleHTTPRequestHandler
import json
import logging
## Configuracion para la Raspberry Pi
from sense_hat import SenseHat
sense = SenseHat()
logging.basicConfig()
USERS = set()
MESSAGE = ''
# Evento usuarios
def users_event():
print('users_event')
# print(json.dumps({"type": "users", "count": len(USERS)}))
if len(USERS) >= 1:
print('Usuario conectado')
print('Usuarios conectados: ' + str(len(USERS)))
else:
print('No hay usuarios conectados')
return json.dumps({"type": "users", "count": len(USERS)})
# Evento mensajes
def value_event():
# print('value_event')
# print(json.dumps({"type": "value", "message": MESSAGE}))
return MESSAGE
async def mensajes(websocket):
global USERS, MESSAGE
try:
# Register user
USERS.add(websocket)
websockets.broadcast(USERS, users_event())
websocket.send(value_event())
# Manejar cambios de estado
async for message in websocket:
MESSAGE = message
websockets.broadcast(USERS, value_event())
print('Mensaje recibido: ' + message)
sense.show_message(message)
finally:
# Desregistrar usuario
USERS.remove(websocket)
websockets.broadcast(USERS, users_event())
# Servidor WebSocket
async def main():
async with websockets.serve(mensajes, "localhost", 8765):
await asyncio.Future() # run forever
if __name__ == "__main__":
print('Servidor WebSocket listo')
asyncio.run(main())