def assign_stream(stream, uuid): uuid = uuid.decode('utf-8') log.debug('Assigning stream to %s' % uuid) Sockets.sockets[uuid] = stream SyncWebSocketHandler.broadcast('NEW_S|' + uuid) stream.set_close_callback(partial(on_close, stream, uuid)) try: stream.read_bytes(4, partial(read_header, stream, uuid)) except StreamClosedError: log.warn('Closed stream for %s' % uuid)
def read_frame(stream, uuid, frame): websocket = Sockets.websockets.get(uuid) if websocket: if websocket.ws_connection is None: log.warn( 'Connection has been closed but websocket is still in map') del Sockets.websockets[uuid] SyncWebSocketHandler.broadcast('RM_WS|' + uuid) else: websocket.write(frame) else: log.error('Web socket is unknown for frame %s' % frame) try: stream.read_bytes(4, partial(read_header, stream, uuid)) except StreamClosedError: log.warn('Closed stream for %s' % uuid)
def on_close(stream, uuid): # None if the user closed the window log.info('uuid %s closed' % uuid) if Sockets.websockets.get(uuid): if Sockets.websockets[uuid].ws_connection is not None: log.info('Telling browser to die') try: Sockets.websockets[uuid].write_message('Die') except: log.warn("Can't tell the browser", exc_info=True) try: Sockets.websockets[uuid].close() except: log.warn("Can't close socket", exc_info=True) del Sockets.websockets[uuid] SyncWebSocketHandler.broadcast('RM_WS|' + uuid) del Sockets.sockets[uuid] SyncWebSocketHandler.broadcast('RM_S|' + uuid)