diff options
| author | Billy Brown | 2017-12-02 20:40:46 +0000 |
|---|---|---|
| committer | Billy Brown | 2017-12-02 20:40:46 +0000 |
| commit | 28f4d872a2be2467fdbd494356f11f518110c6a5 (patch) | |
| tree | e2a3c8e9b897d9488992090a60a6acd0c974f31f /client/client.py | |
| parent | Ball is reset after goal (diff) | |
| download | longpong-28f4d872a2be2467fdbd494356f11f518110c6a5.tar.gz longpong-28f4d872a2be2467fdbd494356f11f518110c6a5.zip | |
Supporting multiple messages at once
Diffstat (limited to 'client/client.py')
| -rw-r--r-- | client/client.py | 40 |
1 files changed, 24 insertions, 16 deletions
diff --git a/client/client.py b/client/client.py index 5614bb9..167a73e 100644 --- a/client/client.py +++ b/client/client.py @@ -14,22 +14,29 @@ class Client: def listen(self): try: - msg = self.sock.recv(4096) - jsonMSG = json.loads(msg.decode()) - print(jsonMSG) - if 'command' in jsonMSG: - val = jsonMSG['command'] - if val == 'start': - self.started = True - self.startupMessage = jsonMSG - self.msgs.append(jsonMSG) - elif val =='stop': - self.stopped = True - self.stoppedMessage = jsonMSG - else: + msg = self.sock.recv(4096).decode() + for message in msg.split('|'): + if len(message) <= 0: + continue + try: + jsonMSG = json.loads(message) + except json.decoder.JSONDecodeError as e: + print(message) + sys.exit(1) + print(jsonMSG) + if 'command' in jsonMSG: + val = jsonMSG['command'] + if val == 'start': + self.started = True + self.startupMessage = jsonMSG self.msgs.append(jsonMSG) - else: - print("It really shouldn't be here") + elif val =='stop': + self.stopped = True + self.stoppedMessage = jsonMSG + else: + self.msgs.append(jsonMSG) + else: + print("It really shouldn't be here") except socket.error as e: pass @@ -60,7 +67,8 @@ class Client: def send_ballchange(self, msg): msg['command'] = "bchange" jsonVal = json.dumps(msg) - self.sock.sendall(str(jsonVal).encode()) + message = str(jsonVal) + "|" + self.sock.sendall(message.encode()) def has_started(self): return self.started |
