I am currently building a client-server-type application with Pythons autobahn (asyncio) framework on both ends. I already have the basic setup of the server, which consists of the server itself and a database managing module. For the client I want to have the networking in a separate module, so I can call <networkingModule>.sendMessage("msg")
for example. Is that a good way to do it?
My problem however is, the code used to initialize the client doesn't give me a real object to work with.
import asyncio
factory = WebSocketClientFactory("ws://localhost:9000", debug = False)
factory.protocol = <ClientProtocolName>
loop = asyncio.get_event_loop()
coro = loop.create_connection(factory, '127.0.0.1', 9000)
loop.run_until_complete(coro)
loop.run_forever()
loop.close()
The class itself is:
class <ClientProtocolName>(WebSocketClientProtocol):
def onConnect(self, response):
print("Server connected: {0}".format(response.peer))
def onOpen(self):
print("WebSocket connection open.")
self.sendInput()
def _sendMessage(self, msg):
self.sendMessage(bytes(msg, "utf-8"))
def onMessage(self, payload, isBinary):
if isBinary:
print("Binary message received: {0} bytes".format(len(payload)))
else:
print("Text message received: {0}".format(payload.decode('utf8')))
def onClose(self, wasClean, code, reason):
print("WebSocket connection closed: {0}".format(reason))
So my question is: Is there a way to build a "networking module" so I can access the sendMessage method from outside and would that be a good way to go about this problem, or should I just stuff all my programm logic into the client itself?