# evennia.server.portal.webclient

Webclient based on websockets.

This implements a webclient with WebSockets (http://en.wikipedia.org/wiki/WebSocket) by use of the autobahn-python package’s implementation (https://github.com/crossbario/autobahn-python). It is used together with evennia/web/media/javascript/evennia_websocket_webclient.js.

All data coming into the webclient is in the form of valid JSON on the form

[“inputfunc_name”, [args], {kwarg}]

which represents an “inputfunc” to be called on the Evennia side with args, **kwargs. The most common inputfunc is “text”, which takes just the text input from the command line and interprets it as an Evennia Command: **[“text”, [“look”], {}]*

class `evennia.server.portal.webclient.``WebSocketClient`(*args, **kwargs)

  Bases: `autobahn.twisted.websocket.WebSocketServerProtocol`, `evennia.server.session.Session`

  Implements the server-side of the Websocket connection.

  `nonce` = 0
  `__init__`(*args, **kwargs)

    Initialize self. See help(type(self)) for accurate signature.

  `get_client_session`()

    Get the Client browser session (used for auto-login based on browser session)

    Returns

      csession (ClientSession) –

      This is a django-specific internal representation

        of the browser session.

  `onOpen`()

    This is called when the WebSocket connection is fully established.

  `disconnect`(reason=None)

    Generic hook for the engine to call in order to disconnect this protocol.

    Parameters

      reason (str or None) – Motivation for the disconnection.

  `onClose`(wasClean, code=None, reason=None)

    This is executed when the connection is lost for whatever reason. it can also be called directly, from the disconnect method.

    Parameters

      - wasClean (bool) – **True** if the WebSocket was closed cleanly.

      - code (int or None) – Close status as sent by the WebSocket peer.

      - reason (str or None) – Close reason as sent by the WebSocket peer.

  `onMessage`(payload, isBinary)

    Callback fired when a complete WebSocket message was received.

    Parameters

      - payload (bytes) – The WebSocket message received.

      - isBinary (bool) – Flag indicating whether payload is binary or UTF-8 encoded text.

  `sendLine`(line)

    Send data to client.

    Parameters

      line (str) – Text to send.

  `at_login`()
  `data_in`(**kwargs)

    Data User > Evennia.

    Parameters

      - text (str) – Incoming text.

      - kwargs (any) – Options from protocol.

    Notes

    At initilization, the client will send the special ‘csessid’ command to identify its browser session hash with the Evennia side.

    The websocket client will also pass ‘websocket_close’ command to report that the client has been closed and that the session should be disconnected.

    Both those commands are parsed and extracted already at this point.

  `send_text`(*args, **kwargs)

    Send text data. This will pre-process the text for color-replacement, conversion to html etc.

    Parameters

      text (str) – Text to send.

    Keyword Arguments

      options (dict) – Options-dict with the following keys understood: - raw (bool): No parsing at all (leave ansi-to-html markers unparsed). - nocolor (bool): Clean out all color. - screenreader (bool): Use Screenreader mode. - send_prompt (bool): Send a prompt with parsed html

  `send_prompt`(*args, **kwargs)
  `send_default`(cmdname, *args, **kwargs)

    Data Evennia -> User.

    Parameters

      - cmdname (str) – The first argument will always be the oob cmd name.

      - *args (any) – Remaining args will be arguments for cmd.

    Keyword Arguments

      options (dict) – These are ignored for oob commands. Use command arguments (which can hold dicts) to send instructions to the client instead.