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)
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()
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)