This commit is contained in:
Samuel Sloniker 2021-06-15 14:21:59 -07:00
parent f2efc515cd
commit 5143593d4c
2 changed files with 60 additions and 10 deletions

View File

@ -105,6 +105,8 @@
errorbox.style.display = 'block' errorbox.style.display = 'block'
canvas.style.display = 'none' canvas.style.display = 'none'
console.error('Server reported error: ' + code + ': ' + msg) console.error('Server reported error: ' + code + ': ' + msg)
} else if (type == 'ack') {
ws.send('ack')
} }
} }
ws.onerror = setup ws.onerror = setup

66
wss.py
View File

@ -7,22 +7,61 @@ import random
import time import time
import shutil import shutil
import tempfile import tempfile
import queue
from websocket_server import WebsocketServer from websocket_server import WebsocketServer
def cycle(): def cycle():
try: try:
changed = hcapi.get_img() changed = hcapi.get_img()
except ImportError as e: except Exception as e:
server.send_message_to_all('err%noconn%Server failed to capture screenshot') #server.send_message_to_all('err%noconn%Server failed to capture screenshot')
for client in clients.values():
client.send('err%noconn%Server failed to capture screenshot')
time.sleep(3) time.sleep(3)
return return
for i in changed: for i in changed:
threading.Thread(target=do_img, args=(i,)).start() threading.Thread(target=do_img, args=(i,)).start()
for client in clients.values():
client.ack()
class Client:
def __init__(self, client, server):
self.client = client
self.server = server
self.queue = queue.Queue()
self.good = True
def send(self, item, name):
self.messages[name] = item
def ack(self):
self.queue.put(['ack'])
def cycle(self):
while not self.good:
pass
item = self.queue.get()
if item[0] == 'msg':
self.server.send_message(self.client, item[1])
else:
self.server.send_message(self.client, 'ack')
self.good = False
def run(self):
while True:
self.cycle()
clients = {}
def do_img(imgname): def do_img(imgname):
server.send_message_to_all(img(imgname)) #server.send_message_to_all(img(imgname))
for client in clients.values():
print(client)
client.send(img(imgname), img)
def img(imgname): def img(imgname):
@ -34,29 +73,38 @@ def img(imgname):
return response return response
def new_client(client, server): def new_client(client, server):
print('got client')
try: try:
imgname = hcapi.get_full_img() imgname = hcapi.get_full_img()
except Exception as e: except Exception as e:
server.send_message(client, 'err%noconn%Server failed to capture screenshot') server.send_message(client, 'err%noconn%Server failed to capture screenshot')
return return
clients[client['id']] = Client(client, server)
with open(imgname, 'rb') as f: with open(imgname, 'rb') as f:
img = f.read() img = f.read()
os.unlink(imgname) os.unlink(imgname)
server.send_message(client, f'pic%0x0%data:imgage/jpeg;base64,{base64.b64encode(img).decode("utf-8")}') server.send_message(client, f'pic%0x0%data:imgage/jpeg;base64,{base64.b64encode(img).decode("utf-8")}')
clients[client['id']] = Client(client, server)
clients[client['id']].ack()
threading.Thread(target=clients[client['id']].run).start()
def do_touch(client, server, message): def do_touch(client, server, message):
password, x, y, w = message.split(' ') action = message.split(' ', 1)[0]
if password == 'password': if action == 'ack':
x, y, w = int(x), int(y), int(w) clients[client['id']].good = True
hcapi.touch(x, y, w)
else: else:
server.send_message(client, f'badpass') _, password, x, y, w = message.split(' ')
if password == 'password':
x, y, w = int(x), int(y), int(w)
hcapi.touch(x, y, w)
else:
clients[client['id']].send(f'badpass')
def do_cycles(): def do_cycles():
while True: while True:
cycle() cycle()
time.sleep(0.2)
tmp = tempfile.mkdtemp(prefix="HCRA-") tmp = tempfile.mkdtemp(prefix="HCRA-")
try: try: