-
Notifications
You must be signed in to change notification settings - Fork 0
/
1server.py
47 lines (38 loc) · 1.42 KB
/
1server.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
from SimpleWebSocketServer import SimpleWebSocketServer, WebSocket
#from chatbot import get_response
from rihanna import get_response
from rihanna import rihanna_voice
from threading import Thread
import ast
import json
class ChatServer(WebSocket):
def handleMessage(self):
# echo message back to client
message = self.data
response = get_response(message)
# self.sendMessage(response) f"{text};{result}"
if response[0] == '{':
try:
response = ast.literal_eval(response)
h1 = Thread(target=self.sendMessage, args=(json.dumps(response),))
h1.start()
except Exception as e:
print(response)
print('server 1: ',e)
else:
try:
h1 = Thread(target=self.sendMessage, args=(response,))
h2 = Thread(target=rihanna_voice, args=(response,))
h1.start()
h2.start()
except Exception as e:
print('server 2: ',e)
def handleConnected(self):
print(self.address, 'connected')
def handleClose(self):
response = {'display': 'server has crashed', 'say': 'server has crashed'}
h1 = Thread(target=self.sendMessage, args=(json.dumps(response),))
h1.start()
print(self.address, 'closed')
server = SimpleWebSocketServer('', 8000, ChatServer)
server.serveforever()