def data_out(self, string='', data=None): """ Data Evennia -> Player access hook. data argument may be used depending on the client-server implementation. """ if data: # treat data? pass # string handling is similar to telnet try: string = utils.to_str(string, encoding=self.encoding) nomarkup = False raw = False if type(data) == dict: # check if we want escape codes to go through unparsed. raw = data.get("raw", False) # check if we want to remove all markup nomarkup = data.get("nomarkup", False) if raw: self.client.lineSend(self.suid, string) else: self.client.lineSend(self.suid, parse_html(string, strip_ansi=nomarkup)) return except Exception, e: logger.log_trace()
def data_out(self, text=None, **kwargs): """ Data Evennia -> Player access hook. webclient flags checked are raw=True - no parsing at all (leave ansi-to-html markers unparsed) nomarkup=True - clean out all ansi/html markers and tokens """ # string handling is similar to telnet try: text = utils.to_str(text if text else "", encoding=self.encoding) raw = kwargs.get("raw", False) nomarkup = kwargs.get("nomarkup", False) if raw: self.client.lineSend(self.suid, text) else: self.client.lineSend(self.suid, parse_html(text, strip_ansi=nomarkup)) return except Exception: logger.log_trace()
class WebSocketProtocol(Protocol, Session): """ This is called when the connection is first established """ def connectionMade(self): """ This is called when the connection is first established. """ client_address = self.transport.client self.init_session("websocket", client_address, self.factory.sessionhandler) self.sessionhandler.connect(self) def disconnect(self, reason=None): """ generic hook for the engine to call in order to disconnect this protocol. """ if reason: self.data_out(text=reason) self.connectionLost(reason) def connectionLost(self, reason): """ this is executed when the connection is lost for whatever reason. it can also be called directly, from the disconnect method """ self.sessionhandler.disconnect(self) self.transport.close() def dataReceived(self, string): """ Method called when data is coming in over the websocket connection. Type of data is identified by a 3-character prefix. OOB - This is an Out-of-band instruction. If so, the remaining string should be a json-packed string on the form {oobfuncname: [[args], {kwargs}], ...} any other prefix (or lack of prefix) is considered plain text data, to be treated like a game input command. """ if string[:3] == "OOB": string = string[3:] try: oobdata = json.loads(string) for (key, argstuple) in oobdata.items(): args = argstuple[0] if argstuple else [] kwargs = argstuple[1] if len(argstuple) > 1 else {} self.data_in(oob=(key, args, kwargs)) except Exception: log_trace("Websocket malformed OOB request: %s" % string) else: # plain text input self.data_in(text=string) def sendLine(self, line): "send data to client" return self.transport.write(line) def data_in(self, text=None, **kwargs): """ Data Websocket -> Server """ self.sessionhandler.data_in(self, text=text, **kwargs) def data_out(self, text=None, **kwargs): """ Data Evennia -> Player. generic hook method for engine to call in order to send data through the websocket connection. valid webclient kwargs: oob=<string> - supply an Out-of-Band instruction. raw=True - no parsing at all (leave ansi-to-html markers unparsed) nomarkup=True - clean out all ansi/html markers and tokens """ try: text = to_str(text if text else "", encoding=self.encoding) except Exception, e: self.sendLine(str(e)) if "oob" in kwargs: oobstruct = self.sessionhandler.oobstruct_parser(kwargs.pop("oob")) self.sendLine("OOB" + json.dumps(oobstruct)) raw = kwargs.get("raw", False) nomarkup = kwargs.get("nomarkup", False) if raw: self.sendLine(text) else: self.sendLine(parse_html(text, strip_ansi=nomarkup))