Created
March 16, 2020 07:16
-
-
Save Rockstar5645/1c20531f1609be6e3dd4de24499e0d3c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
client_list = {} | |
async def chat_room(reader, writer): | |
addr = writer.get_extra_info('peername') | |
print(f'got a connection from client at: {addr}') | |
data = await reader.read(100) | |
if not data: | |
del client_list[addr] | |
if addr in client_list: | |
# This is a socket we've already connected to | |
for client_addr, client_writer in client_list: | |
if client_writer is not writer: | |
client_writer.write(data) | |
await client_writer.drain() | |
writer.close() | |
else: | |
# Add this writer into our client table | |
client_list[addr] = writer | |
async def main(): | |
server = await asyncio.start_server( | |
chat_room, '127.0.0.1', 8888) | |
addr = server.sockets[0].getsockname() | |
print(f'Serving on {addr}') | |
async with server: | |
await server.serve_forever() | |
asyncio.run(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment