def __init__(self, ip, port, username, password, loop=asyncio.get_event_loop()): Client.__init__(self, retry=True, loop=loop) self.setTextHandler(self.recv_data) self.connectTo(ip, port=port, useSsl=True, url="wss://{}:{}/?username={}&password={}". format(ip, port, username, password), protocols=['mfi-protocol']) self.ip = ip self.port = port
def __init__(self, name, address, port, use_ssl=False): CameraBase.__init__(self, name, maxsize=5) self.address = address self.port = port from wss import Client client = Client(retry=True) client.setTextHandler(self.img_received) client.connectTo(self.address, self.port, useSsl=use_ssl)
def __init__(self, camera_name, address="localhost", port=9004, usessl=False, loop=None): self.debug = False self.msgRecieved = {} self.camera_name = camera_name self.address = address self.port = port self.usessl = usessl self.client = Client(retry=True, loop=loop) self.client.setTextHandler(self.txtHndler) self.client.setOpenHandler(self.connection_opened) self.openHandler = None
class RecognitionClient: def __init__(self, camera_name, address="localhost", port=9004, usessl=False, loop=None): self.debug = False self.msgRecieved = {} self.camera_name = camera_name self.address = address self.port = port self.usessl = usessl self.client = Client(retry=True, loop=loop) self.client.setTextHandler(self.txtHndler) self.client.setOpenHandler(self.connection_opened) self.openHandler = None def connect(self): self.client.connectTo(self.address, self.port, useSsl=self.usessl) def debug_msg(self, msg): if self.debug == True: print("debug: " + msg) def connection_opened(self): try: if self.camera_name: self.select_camera(self.camera_name) if self.openHandler: self.openHandler() except: import sys, traceback exc_type, exc_value, exc_traceback = sys.exc_info() traceback.print_tb(exc_traceback, limit=1, file=sys.stdout) traceback.print_exception(exc_type, exc_value, exc_traceback, limit=6, file=sys.stdout) def setOpenHandler(self, callback): self.openHandler = callback def select_camera(self, camera_name): self.client.sendTextMsg(Messages.select_camera(camera_name)) def list_users(self, filter=None): self.client.sendTextMsg(Messages.list_users(filter)) def list_users_with_level(self, level): self.client.sendTextMsg(Messages.list_users_with_level(level)) def txtHndler(self, data): self.debug_msg("Trying to parse text message: {}".format(data)) data_obj = json.loads(data) if "signal" in data_obj: self.hndlSignal(data_obj) else: self.debug_msg("unhandled message: {}".format(data)) def hndlSignal(self, data_obj): msg_class = data_obj["signal"] if msg_class in C.class_map: c = C.class_map[msg_class] msg = c(data_obj) if c in self.msgRecieved: self.msgRecieved[c](msg) else: self.debug_msg("signal no handler for signal {}".format(c)) else: self.debug_msg( "no class defined to parse signal {}".format(msg_class)) def setMessageReceived(self, msg_type, callback): self.msgRecieved[msg_type] = callback
def test_server_client(): loop = asyncio.get_event_loop() s = Server(port = 9000, debug = True, useSsl=False) to_send = "hello world" to_reply = "foo bar" received = False server_received = False @asyncio.coroutine def sendData(): while True: try: print("trying to broadcast to {} clients...".format(len(s.clients))) s.broadcast(to_send, False) sent = True except: exc_type, exc_value, exc_traceback = sys.exc_info() traceback.print_tb(exc_traceback, limit=1, file=sys.stdout) traceback.print_exception(exc_type, exc_value, exc_traceback, limit=2, file=sys.stdout) yield from asyncio.sleep(0.1) loop.create_task(sendData()) def onMessage(msg, client): print("received message: {}".format(msg)) assert msg == bytes(to_reply, 'utf-8') server_received = True s.setTextHandler(onMessage) s.start() client = Client(retry=True, loop=loop) def textHandler(msg): print(msg) assert msg.decode('utf-8') == to_send received = True def opened(): print("client connected") print(client.connected) client.sendTextMsg(to_reply) def closed(): print("connection closed") client.debug = True client.setTextHandler(textHandler) client.setOpenHandler(opened) client.setCloseHandler(closed) client.connectTo("localhost", 9000, useSsl=False) def stop_loop(): loop.stop() loop.call_later(5, stop_loop) loop.run_forever() assert client.connected assert sent assert received assert server_received
def registerClient(self, hndl): print("connected!") Client.registerClient(self, hndl)
import trollius as asyncio import base64 from wss import Client def showImage(payload): payload = base64.b64decode(payload) img = np.frombuffer(payload, dtype='uint8') img = cv2.imdecode(img, cv2.IMREAD_COLOR) cv2.imshow("image", img) k = cv2.waitKey(1) if __name__ == '__main__': import argparse parser = argparse.ArgumentParser() parser.add_argument('address', help="address", default="localhost", nargs="?") parser.add_argument('port', help="port", default=9000, nargs="?") parser.add_argument('--ssl', dest="usessl", help="use ssl.", action='store_true') args = parser.parse_args() client = Client() if client.connectTo(args.address, args.port, useSsl=args.usessl): print("Connected!") else: print("Failed to connect") client.setTextHandler(showImage) asyncio.get_event_loop().run_forever()
payload = base64.b64decode(payload) img = np.frombuffer(payload, dtype='uint8') img = cv2.imdecode(img, cv2.IMREAD_COLOR) cv2.imshow("image", img) k = cv2.waitKey(1) if __name__ == '__main__': import argparse parser = argparse.ArgumentParser() parser.add_argument('address', help="address", default="localhost", nargs="?") parser.add_argument('port', help="port", default=9000, nargs="?") parser.add_argument('--ssl', dest="usessl", help="use ssl.", action='store_true') args = parser.parse_args() client = Client() if client.connectTo(args.address, args.port, useSsl=args.usessl): print("Connected!") else: print("Failed to connect") client.setTextHandler(showImage) asyncio.get_event_loop().run_forever()
ret, img = cap.read() if not ret: break avgColor.handleImg(img) cv2.imshow("image", img) k = cv2.waitKey(1) print("has image!") yield asyncio.From(asyncio.sleep(1/30)) if __name__ == '__main__': import argparse from wss import Client parser = argparse.ArgumentParser() args = parser.parse_args() client = Client() avgColor = AverageColor() asyncio.get_event_loop().create_task(showImage(avgColor)) asyncio.get_event_loop().run_forever()
class RecognitionClient: def __init__(self, camera_name, address="localhost", port=9004, usessl=False, loop=None): self.debug = False self.msgRecieved = {} self.camera_name = camera_name self.address = address self.port = port self.usessl = usessl self.client = Client(retry=True, loop=loop) self.client.setTextHandler(self.txtHndler) self.client.setOpenHandler(self.connection_opened) self.openHandler = None def connect(self): self.client.connectTo(self.address, self.port, useSsl=self.usessl) def debug_msg(self, msg): if self.debug == True: print("debug: " + msg) def connection_opened(self): try: if self.camera_name: self.select_camera(self.camera_name) if self.openHandler: self.openHandler() except: import sys, traceback exc_type, exc_value, exc_traceback = sys.exc_info() traceback.print_tb(exc_traceback, limit=1, file=sys.stdout) traceback.print_exception(exc_type, exc_value, exc_traceback, limit=6, file=sys.stdout) def setOpenHandler(self, callback): self.openHandler = callback def select_camera(self, camera_name): self.client.sendTextMsg(Messages.select_camera(camera_name)) def list_users(self, filter=None): self.client.sendTextMsg(Messages.list_users(filter)) def list_users_with_level(self, level): self.client.sendTextMsg(Messages.list_users_with_level(level)) def txtHndler(self, data): self.debug_msg("Trying to parse text message: {}".format(data)) data_obj = json.loads(data) if "signal" in data_obj: self.hndlSignal(data_obj) else: self.debug_msg("unhandled message: {}".format(data)) def hndlSignal(self, data_obj): msg_class = data_obj["signal"] if msg_class in C.class_map: c = C.class_map[msg_class] msg = c(data_obj) if c in self.msgRecieved: self.msgRecieved[c](msg) else: self.debug_msg("signal no handler for signal {}".format(c)) else: self.debug_msg("no class defined to parse signal {}".format(msg_class)) def setMessageReceived(self, msg_type, callback): self.msgRecieved[msg_type] = callback