Advertisement
MarkUa

Untitled

Apr 16th, 2020
1,057
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.32 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3.  
  4. import asyncio
  5. import json
  6. # The set of clients connected to this server. It is used to distribute
  7. # messages.
  8. clients = {} #: {websocket: name}
  9. chats = set()
  10. # { "chat_name" : [ ("time", "author", "massage") ] }
  11. messages = dict()
  12. users = {}
  13. @asyncio.coroutine
  14. def client_handler(websocket, path):
  15.  
  16.     print(path)
  17.     print('New client', websocket)
  18.     print(' ({} existing clients)'.format(len(clients)))
  19.  
  20.     # The first line from the client is the name
  21.     data =  yield from websocket.recv()
  22.     print(data)
  23.     data = json.loads(data)
  24.  
  25.     if data["chat"] not in chats:
  26.         chats.add(data["chat"])
  27.  
  28.         if data["name"] not in users:
  29.             users[data["name"]] = {"password": data["password"], "chats":  [ data["chat"] ]  }
  30.         else:
  31.             users[data["name"]]["chats"].append(data["chat"])
  32.         yield from websocket.send('Welcome to websocket-chat {} , {}'.format(data["chat"], data["name"]) )
  33.         clients[websocket] = data
  34.  
  35.         for client, _ in clients.items():
  36.             if  _["chat"] == data["chat"]:
  37.                 yield from client.send(data["name"] + ' has joined the chat '+ data["chat"])
  38.     elif data["chat"] in chats:
  39.         if data["name"] not  in users:
  40.             users[data["name"]] = {"password": data["password"], "chats":  [ data["chat"] ]  }
  41.  
  42.             yield from websocket.send('Welcome to websocket-chat {} , {}'.format(data["chat"], data["name"]))
  43.             clients[websocket] = data
  44.  
  45.             for client, _ in clients.items():
  46.                 if _["chat"] == data["chat"]:
  47.                     yield from client.send(data["name"] + ' has joined the chat ' + data["chat"])
  48.  
  49.         else:
  50.             if data["password"] == users[data["name"]]["password"]:
  51.                 yield from websocket.send('Welcome to websocket-chat {} , {}'.format(data["chat"], data["name"]))
  52.                 clients[websocket] = data
  53.  
  54.                 for client, _ in clients.items():
  55.                     if _["chat"] == data["chat"]:
  56.                         yield from client.send(data["name"] + ' has joined the chat ' + data["chat"])
  57.             else:
  58.                 yield from websocket.send('not correct password')
  59.     # Handle messages from this client
  60.     while True:
  61.        # for i in chats:
  62.             print(11)
  63.             message = yield from websocket.recv()
  64.             print(message)
  65.             if message is None:
  66.                 their_name = clients[websocket]["name"]
  67.                 del clients[websocket]
  68.                 print('Client closed connection', websocket)
  69.                 for client, _ in clients.items():
  70.                     yield from client.send(their_name + ' has left the chat')
  71.                 break
  72.  
  73.             # Send message to all clients
  74.             for client, _ in clients.items():
  75.                 print(str(client)+" clients[websocket][chat] " + str(clients[websocket]["chat"]) + " " +  _["chat"])
  76.                 if str(clients[websocket]["chat"]) ==  _["chat"]:
  77.                     yield from client.send('{}: {}'.format(clients[websocket]["name"], message))
  78.  
  79.  
  80. LISTEN_ADDRESS = ('0.0.0.0', 8080)
  81.  
  82. import websockets
  83. start_server = websockets.serve(client_handler, *LISTEN_ADDRESS)
  84.  
  85. import asyncio
  86. asyncio.get_event_loop().run_until_complete(start_server)
  87. asyncio.get_event_loop().run_forever()
  88. 5
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement