Stop client on loss of connection

This commit is contained in:
Joscha 2017-04-18 13:46:39 +00:00
parent 5029f376a6
commit ac65d9ab0a

View file

@ -65,8 +65,13 @@ class Client():
) )
self.inputthread.start() self.inputthread.start()
# update screen until stopped
while not self.stopping: while not self.stopping:
self.update_screen()
if self.logfile:
self.save_log(self.logfile)
def update_screen(self):
self._drawevent.wait() self._drawevent.wait()
self._drawevent.clear() self._drawevent.clear()
@ -84,9 +89,6 @@ class Client():
curses.doupdate() curses.doupdate()
if self.logfile:
self.save_log(self.logfile)
def redraw(self): def redraw(self):
self._drawevent.set() self._drawevent.set()
@ -108,10 +110,7 @@ class Client():
elif mode == self.MOVE_MAP: elif mode == self.MOVE_MAP:
self.map_.scroll(x*20, y*10) self.map_.scroll(x*20, y*10)
def input_thread(self, scr): def handle_input(self, i):
while True:
i = scr.get_wch()
if i == "\x1b": self.stop() if i == "\x1b": self.stop()
elif i == curses.KEY_F1: self.movement = self.MOVE_NORMAL elif i == curses.KEY_F1: self.movement = self.MOVE_NORMAL
elif i == curses.KEY_F2: self.movement = self.MOVE_FAST elif i == curses.KEY_F2: self.movement = self.MOVE_FAST
@ -153,16 +152,21 @@ class Client():
elif isinstance(i, str) and len(i) == 1 and ord(i) > 31 and (i not in string.whitespace or i == " "): elif isinstance(i, str) and len(i) == 1 and ord(i) > 31 and (i not in string.whitespace or i == " "):
self.map_.write(i) self.map_.write(i)
def input_thread(self, scr):
while True:
i = scr.get_wch()
self.handle_input(i)
self.log(f"K: {i!r}") self.log(f"K: {i!r}")
def connection_thread(self): def connection_thread(self):
while True:
try: try:
while True:
j = self._ws.recv() j = self._ws.recv()
if j: if j:
self.handle_json(json.loads(j)) self.handle_json(json.loads(j))
except (WSException, ConnectionResetError, OSError): except (WSException, ConnectionResetError, OSError):
#self.stop() self._ws = None
self.stop()
return return
def handle_json(self, message): def handle_json(self, message):
@ -172,7 +176,9 @@ class Client():
def stop(self): def stop(self):
self.stopping = True self.stopping = True
if self._ws:
self._ws.close() self._ws.close()
self._ws = None
self.redraw() self.redraw()
def request_chunks(self, coords): def request_chunks(self, coords):