chr15m / chr15m/PodSixNet

Podsixnet with Asyncio.

Open
#36 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
175
Forks
34
PR merge metrics
No merged PRs in 30d

Description

Hi. First of all thank you for this excellent project. Wanting to implement a chat following the example provided here, I would like not to use threads, as it is also recommended, but to use asyncio, which as I understand it, is the best method to manage coroutines. I modified the chatclient.py file and it does not give an error, however the chat messages are not sent between the various clients. Can you help me understand what I need to change?
Thank you. The example is the one you provided, only with a few minor modifications.

from __future__ import print_function
import asyncio
import sys
from time import sleep
from sys import stdin, exit
from PodSixNet.Connection import connection, ConnectionListener

from _thread import *

class Client(ConnectionListener):
def __init__(self, host, port):
self.Connect((host, port))
print("Chat client started")
print("Ctrl-C to exit")
# get a nickname from the user before starting
print("Enter your nickname: ")
connection.Send({"action": "nickname", "nickname": stdin.readline().rstrip("\n")})
# launch our threaded input loop
loop = asyncio.get_event_loop()


async def Loop(self):
connection.Pump()
self.Pump()

async def InputLoop(self):
# horrid threaded input loop
# continually reads from stdin and sends whatever is typed to the server
while 1:
connection.Send({"action": "message", "message": stdin.readline().rstrip("\n")})

def Network_players(self, data):
print("*** players: " + ", ".join([p for p in data['players']]))

def Network_message(self, data):
print(data['who'] + ": " + data['message'])

# built in stuff

def Network_connected(self, data):
print("You are now connected to the server")

def Network_error(self, data):
print('error:', data['error'][1])
connection.Close()

def Network_disconnected(self, data):
print('Server disconnected')
exit()

if __name__ == '__main__':
if len(sys.argv) != 2:
print("Usage:", sys.argv[0], "host:port")
print("e.g.", sys.argv[0], "localhost:31425")
else:
host, port = sys.argv[1].split(":")
c = Client(host, int(port))
while 1:
asyncio.run(c.Loop())
sleep(0.001)

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by comparing the modified chatclient.py with the provided chat example, focusing on Client.Loop, InputLoop, connection.Pump, and asyncio.run. Done means an asyncio-based client reliably sends chat messages between multiple clients without relying on the threaded input loop.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
networking
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.