aboutsummaryrefslogtreecommitdiff
path: root/client/Client.py
diff options
context:
space:
mode:
authorBilly Brown2017-12-02 15:15:25 +0000
committerBilly Brown2017-12-02 15:15:25 +0000
commit4ebc576c2a30d5b0d16a42acdb81f718f7f00ddd (patch)
treeeaa3f84f5152167c965fe3bf3e1e874e4c01801f /client/Client.py
parentGame initial commit (diff)
parentClient has been created (diff)
downloadlongpong-4ebc576c2a30d5b0d16a42acdb81f718f7f00ddd.tar.gz
longpong-4ebc576c2a30d5b0d16a42acdb81f718f7f00ddd.zip

Merge remote-tracking branch ‘origin/PythonClientConnection’

Diffstat (limited to 'client/Client.py')
-rw-r--r--client/Client.py64
1 files changed, 64 insertions, 0 deletions
diff --git a/client/Client.py b/client/Client.py
new file mode 100644
index 0000000..bddb56b
--- /dev/null
+++ b/client/Client.py
@@ -0,0 +1,64 @@
+import socket
+import sys
+import json
+
+class Client:
+ def __init__(self,ip,port,n):
+ self.started = False
+ self.stopped = False
+ self.msgs =[]
+ self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ self.sock.connect((ip,port))
+ self.n = n
+ self.client_sock.setblocking(False)
+
+ def listen(self):
+ try:
+ msg = client_sock.recv(4096)
+ jsonMSG = json.loads(msg)
+ print(jsonMSG)
+ if 'command' in data:
+ val = jsonMSG['command']
+ if val == 'start':
+ self.started = True
+ self.startupMessage = jsonMSG
+ elif val =='stop':
+ self.stopped = True
+ self.stoppedMessage = jsonMSG
+ else:
+ msgs.append(jsonMSG)
+ else:
+ print("It really shouldn't be here")
+
+ except socket.error, e:
+
+ def get_messages(self):
+ ret = self.msgs[:]
+ self.msgs = []
+
+ def send_init(self,screen):
+ inp = {}
+ inp['nValue'] = self.n;
+ inp['screenSize'] = str(screen)
+ self.client_sock.send(str(json.dumps(inp)))
+
+ def send_start(self):
+ msg ={'command':'start'}
+ jsonVal = json.dumps(msg)
+ self.client_sock.send(str(jsonVal))
+
+ def send_stop(self):
+ msg ={'command':'stop'}
+ jsonVal = json.dumps(msg)
+ self.client_sock.send(str(jsonVal))
+
+ def send_ballchange(self,message):
+ msg['command'] = "bchange"
+ jsonVal = json.dumps(msg)
+ self.client_sock.send(str(jsonVal))
+
+ def has_started(self):
+ return self.started
+
+ def has_stopped(self):
+ return self.stopped