-
Notifications
You must be signed in to change notification settings - Fork 11
/
chatroom.py
61 lines (44 loc) · 2.1 KB
/
chatroom.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
#!/usr/bin/env python
import asyncio, json, websockets, sys, time
USERS = set()
def mesej_event(username, roomiden, textmesg):
return json.dumps({"username": username, "roomiden": roomiden, "textmesg": textmesg})
async def notify_mesej(username, roomiden, textmesg):
if USERS:
message = mesej_event(username, roomiden, textmesg)
await asyncio.wait([user.send(message) for user in USERS])
async def register(websocket):
USERS.add(websocket)
print(" > [" + str(time.ctime()) + "] [USERJOIN] User just joined the Sanctuary")
async def unregister(websocket):
USERS.remove(websocket)
dir(websocket)
print(" > [" + str(time.ctime()) + "] [USERLEFT] User just left the Sanctuary")
async def chatroom(websocket, path):
await register(websocket)
try:
async for message in websocket:
data = json.loads(message)
print(" > [" + str(time.ctime()) + "] [" + str(data["roomiden"]) + "] User '" + str(data["username"]) + "' sends message '" + str(data["textmesg"]) + "'")
await notify_mesej(data["username"], data["roomiden"], data["textmesg"])
finally:
await unregister(websocket)
def servenow(netpdata="127.0.0.1", chatport="9696"):
try:
print(" > [" + str(time.ctime()) + "] [HOLAUSER] Sanctuary was started up on 'ws://" + str(netpdata) + ":" + str(chatport) + "/'")
start_server = websockets.serve(chatroom, netpdata, int(chatport))
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
except KeyboardInterrupt:
print("\n" + " > [" + str(time.ctime()) + "] [SEEUSOON] Sanctuary was shut down")
sys.exit()
def mainfunc(chatport, netprotc):
print(" > [" + str(time.ctime()) + "] [HOLAUSER] Starting Sanctuary...")
netpdata = ""
if netprotc == "ipprotv6":
print(" > [" + str(time.ctime()) + "] [HOLAUSER] IP version : 6")
netpdata = "::"
elif netprotc == "ipprotv4":
print(" > [" + str(time.ctime()) + "] [HOLAUSER] IP version : 4")
netpdata = "0.0.0.0"
servenow(netpdata, chatport)