/
chat_server.py
40 lines (31 loc) · 1.1 KB
/
chat_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
from time import sleep
from async import Async
from websocketremotefunctioncaller import WebSocketRemoteFunctionCaller
class MyWebSocketRemoteFunctionCaller(WebSocketRemoteFunctionCaller):
def onopen(self, client):
print "%s joined the chat." % client[1]
for dest_client in ws.clients():
if dest_client != client:
Async(joined_chat, str(client[1]), dest_client)
if __name__ == "__main__":
ws = MyWebSocketRemoteFunctionCaller()
ws.start('127.0.0.1', '8080');
# Functions on client
@ws.client_function()
def from_others(agent, message): pass
@ws.client_function()
def joined_chat(agent): pass
# Functions client can access
@ws.server_function()
def end():
ws.stop()
ws.server.shutdown()
@ws.server_function()
def message(client, message):
for dest_client in ws.clients():
if dest_client != client:
Async(from_others, str(client[1]), message, dest_client)
while True:
sleep(5)
if not ws.running:
break