-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.py
executable file
·133 lines (111 loc) · 4.53 KB
/
Client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# -*- coding: utf-8 -*-
import socket
from MessageReceiver import MessageReceiver
import json
class Client:
"""
This is the chat client class
"""
def __init__(self, host, server_port):
"""
This method is run when creating a new Client object
"""
# Set up the socket connection to the server
self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.host = host
self.server_port = server_port
self.msgRec = MessageReceiver(self,self.connection)
self.hasLoggedOn = False
self.run()
def run(self):
# Initiate the connection to the server
self.connection.connect((self.host, self.server_port))
self.msgRec.start()
print "Velkommen til denne chatte-appen. Skriv <-login> hvis du vil logge inn. Hvis du har problemer skriv <-help>"
print ""
while True:
command = raw_input('')
if command == "-login":
print 'Skriv inn ditt onskede brukernavn'
username = raw_input('')
data = {"request":"login","content":username}
try:
package = json.dumps(data)
self.send_payload(package)
self.hasLoggedOn = True
except UnicodeDecodeError:
print("Ikke bruk norske bokstaver.")
continue
elif command == "-help":#skal faa hjelpemelding fra server
data = {"request":"help","content":""}
package = json.dumps(data)
self.send_payload(package)
elif command == "-names" and self.hasLoggedOn:
#faa navn fra server
data = {"request":"names","content":""}
package = json.dumps(data)
self.send_payload(package)
elif command == "-logout" and self.hasLoggedOn:
data = {"request":"logout","content":username}
package = json.dumps(data)
self.send_payload(package)
self.disconnect()
self.hasLoggedOn = False
print "Du er naa logget ut"
elif command == "-history" and self.hasLoggedOn:
#faa historie fra serveren
data = {"request":"history","content":""}
package = json.dumps(data)
self.send_payload(package)
else:
if self.hasLoggedOn:
data = {"request":"msg","content":command}
try:
package = json.dumps(data)
self.send_payload(package)
except UnicodeDecodeError:
print("Ikke bruk norske bokstaver.")
continue
else:
print "Du maa vaere logget inn for aa gjore det"
def disconnect(self):
# TODO: Handle disconnection
self.connection.close()
def receive_message(self, message):
# TODO: Handle incoming message
if type(message) != str:
received_string = self.connection.recv(4096)
try:
jsonRec = json.loads(received_string)
timestamp = jsonRec["timestamp"]
sender = jsonRec["sender"]
response = jsonRec["response"]
content = jsonRec["content"]
except ValueError:
print("Not JSON-Object, trying again.")
elif message:
jsonRec = json.loads(message)
timestamp = jsonRec["timestamp"]
sender = jsonRec["sender"]
response = jsonRec["response"]
content = jsonRec["content"]
if response == "message":
msg = "[" + timestamp + " " + sender + "] " + content
print msg
elif response == "history":
msg = "[" + timestamp + " " + sender + "] " + content
print msg
else:
print content
# elif response == "error":
# print content
def send_payload(self, data):
# TODO: Handle sending of a payload
self.connection.send(data)
if __name__ == '__main__':
"""
This is the main method and is executed when you type "python Client.py"
in your terminal.
No alterations is necessary
."""
client = Client('localhost', 9998)