Commit a95914e8 authored by Taddeüs Kroes's avatar Taddeüs Kroes

Code cleanup

parent 0de6f644
...@@ -24,8 +24,8 @@ class Server(object): ...@@ -24,8 +24,8 @@ class Server(object):
def run(self): def run(self):
while True: while True:
try: try:
client_socket, address = self.sock.accept() sock, address = self.sock.accept()
client = Client(self, client_socket, address) client = Client(self, sock, address)
client.handshake() client.handshake()
self.clients.append(client) self.clients.append(client)
logging.info('Registered client %s', client) logging.info('Registered client %s', client)
...@@ -38,8 +38,12 @@ class Server(object): ...@@ -38,8 +38,12 @@ class Server(object):
except Exception as e: except Exception as e:
logging.error(format_exc(e)) logging.error(format_exc(e))
def remove_client(self, client, code, reason):
self.clients.remove(client)
self.onclose(client, code, reason)
def onopen(self, client): def onopen(self, client):
logging.debug('Opened socket to client %s' % client) logging.debug('Opened socket to %s' % client)
def onmessage(self, client, message): def onmessage(self, client, message):
logging.debug('Received %s from %s' % (message, client)) logging.debug('Received %s from %s' % (message, client))
...@@ -50,14 +54,23 @@ class Server(object): ...@@ -50,14 +54,23 @@ class Server(object):
def onpong(self, client, payload): def onpong(self, client, payload):
logging.debug('Received pong "%s" from %s' % (payload, client)) logging.debug('Received pong "%s" from %s' % (payload, client))
def onclose(self, client): def onclose(self, client, code, reason):
logging.debug('Closed socket to client %s' % client) msg = 'Closed socket to %s' % client
if code is not None:
msg += ' [%d]' % code
if len(reason):
msg += ' "%s"' % reason
logging.debug(msg)
class Client(WebSocket): class Client(WebSocket):
def __init__(self, server, sock, address): def __init__(self, server, sock, address):
super(Client, self).__init__(sock, address) super(Client, self).__init__(sock)
self.server = server self.server = server
self.address = address
def onopen(self): def onopen(self):
self.server.onopen(self) self.server.onopen(self)
...@@ -71,8 +84,8 @@ class Client(WebSocket): ...@@ -71,8 +84,8 @@ class Client(WebSocket):
def onpong(self, payload): def onpong(self, payload):
self.server.onpong(self, payload) self.server.onpong(self, payload)
def onclose(self): def onclose(self, code, reason):
self.server.onclose(self) self.server.remove_client(self, code, reason)
def __str__(self): def __str__(self):
return '<Client at %s:%d>' % self.address return '<Client at %s:%d>' % self.address
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment