2021-09-30 01:21:18 +02:00
|
|
|
__author__ = "Kieran Osborne"
|
|
|
|
__version__ = "0.0.1"
|
|
|
|
__status__ = "Development"
|
|
|
|
|
|
|
|
if (__name__ == "__main__"):
|
2021-10-05 01:55:17 +02:00
|
|
|
import asyncio
|
2021-10-01 01:16:27 +02:00
|
|
|
import chattle
|
2021-09-30 01:21:18 +02:00
|
|
|
import config
|
2021-09-30 02:25:55 +02:00
|
|
|
import socket
|
|
|
|
import sys
|
2021-09-30 01:21:18 +02:00
|
|
|
|
2021-10-01 01:16:27 +02:00
|
|
|
username = input("username: ")
|
2021-09-30 02:25:55 +02:00
|
|
|
address = (config.host, config.port)
|
2021-09-30 01:21:18 +02:00
|
|
|
|
2021-09-30 02:25:55 +02:00
|
|
|
print("Starting connection to", address)
|
2021-09-30 01:21:18 +02:00
|
|
|
|
2021-09-30 02:25:55 +02:00
|
|
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server_socket:
|
|
|
|
server_socket.connect_ex(address)
|
2021-10-05 01:55:17 +02:00
|
|
|
print(server_socket.recv(config.message_max).decode("utf-8"))
|
2021-09-30 01:21:18 +02:00
|
|
|
|
2021-10-05 01:55:17 +02:00
|
|
|
async def listen_server():
|
|
|
|
response_data = server_socket.recv(config.message_max)
|
2021-09-30 01:21:18 +02:00
|
|
|
|
2021-10-05 01:55:17 +02:00
|
|
|
while response_data:
|
|
|
|
print(response_data.decode("utf-8"))
|
2021-09-30 01:21:18 +02:00
|
|
|
|
2021-10-05 01:55:17 +02:00
|
|
|
response_data = server_socket.recv(config.message_max)
|
2021-10-01 01:16:27 +02:00
|
|
|
|
2021-10-05 01:55:17 +02:00
|
|
|
# Listening for server responses has to be done on a separate unit of computation, such as an asynchronous
|
|
|
|
# operation, so as not to block the console from working.
|
|
|
|
asyncio.get_event_loop().create_task(listen_server())
|
2021-10-01 01:16:27 +02:00
|
|
|
|
2021-10-05 01:55:17 +02:00
|
|
|
is_running = True
|
2021-09-30 02:25:55 +02:00
|
|
|
|
2021-10-05 01:55:17 +02:00
|
|
|
while is_running:
|
|
|
|
line = sys.stdin.readline().strip()
|
2021-10-01 01:16:27 +02:00
|
|
|
|
2021-10-05 01:55:17 +02:00
|
|
|
if line:
|
|
|
|
# Messages produced by this client are written to the terminal locally, rather than sending it to
|
|
|
|
# server to then receive it back.
|
|
|
|
if not line.startswith("/"):
|
|
|
|
sys.stdout.write("<You> ")
|
|
|
|
sys.stdout.write(line)
|
|
|
|
sys.stdout.write("\n")
|
|
|
|
sys.stdout.flush()
|
2021-09-30 02:25:55 +02:00
|
|
|
|
2021-10-05 01:55:17 +02:00
|
|
|
server_socket.send(chattle.encode_message(username, line))
|
|
|
|
|
|
|
|
if (line.lower() == "/quit"):
|
|
|
|
# Handle exiting on the client-side.
|
|
|
|
is_running = False
|
2021-09-30 01:21:18 +02:00
|
|
|
|