def ping(self): print '--- PING (CONTROLLER) ---' if self._WSClient.hasStatus('OPEN'): _WSEncoder = WSEncoder() try: bytes = _WSEncoder.ping('Application data') except ValueError as error: self._WSClient._WSServer.remove(self._WSClient) self.kill(1011, 'WSEncoder error: ' + str(error)) else: self._WSClient.send(bytes)
def kill(self, code=1000, error=''): print '--- KILL (CONTROLLER) ---', repr(self._WSClient.conn) if not self._WSClient.hasStatus('CLOSED'): _WSEncoder = WSEncoder() data = struct.pack('!H', code) if len(error): data += error print '--- KILL FRAME ---', code, error, repr(self._WSClient.conn) try: bytes = _WSEncoder.close(data) except ValueError as error: self._WSClient.close() else: self._WSClient.send(bytes) self._WSClient.close()
def run(self, ctrl, data): print '--- CONTROLLER ---', repr(self._WSClient.conn) _WSEncoder = WSEncoder() # CONTROLS if ctrl['opcode'] == 0x9: # PING print '--- PING FRAME ---', repr(self._WSClient.conn) try: bytes = _WSEncoder.pong('Application data') except ValueError as error: self._WSClient._WSServer.remove(self._WSClient) self.kill(1011, 'WSEncoder error: ' + str(error)) else: self._WSClient.send(bytes) if ctrl['opcode'] == 0xA: # PONG print '--- PONG FRAME ---', repr(self._WSClient.conn) if len(data): print 'Pong frame datas:', str(data) if ctrl['opcode'] == 0x8: # CLOSE print '--- CLOSE FRAME ---', repr(self._WSClient.conn) self._WSClient._WSServer.remove(self._WSClient) # closing was initiated by server if self._WSClient.hasStatus('CLOSING'): self._WSClient.close() # closing was initiated by client if self._WSClient.hasStatus('OPEN'): self._WSClient.setStatus('CLOSING') self.kill(1000, 'Goodbye !') # the two first bytes MUST contains the exit code, follow optionnaly with text data not shown to clients if len(data) >= 2: code, data = self.array_shift(data,2) status = '' if code in WSSettings.CLOSING_CODES: print 'Closing frame code:', code if len(data): print 'Closing frame data:', data # DATAS if ctrl['opcode'] == 0x1: # TEXT print '--- TEXT FRAME ---', repr(self._WSClient.conn) if len(data): try: bytes = _WSEncoder.text(data) except ValueError as error: self._WSClient._WSServer.remove(self._WSClient) self.kill(1011, 'WSEncoder error: ' + str(error)) else: # send incoming message to all clients self._WSClient._WSServer.send(bytes) # test ping/pong self.ping() if ctrl['opcode'] == 0x0: # CONTINUATION print '--- CONTINUATION FRAME ---', repr(self._WSClient.conn) pass if ctrl['opcode'] == 0x2: # BINARY print '--- BINARY FRAME ---', repr(self._WSClient.conn) pass