# evennia.server.portal.webclient_ajax

AJAX/COMET fallback webclient

The AJAX/COMET web client consists of two components running on twisted and django. They are both a part of the Evennia website url tree (so the testing website might be located on http://localhost:4001/, whereas the webclient can be found on http://localhost:4001/webclient.)

/webclient - this url is handled through django’s template

  system and serves the html page for the client itself along with its javascript chat program.

/webclientdata - this url is called by the ajax chat using

  POST requests (long-polling when necessary) The WebClient resource in this module will handle these requests and act as a gateway to sessions connected over the webclient.

class `evennia.server.portal.webclient_ajax.``LazyEncoder`(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)

  Bases: `json.encoder.JSONEncoder`

  `default`(obj)

    Implement this method in a subclass such that it returns a serializable object for **o**, or calls the base implementation (to raise a **TypeError**).

    For example, to support arbitrary iterators, you could implement default like this:

        def default(self, o):
            try:
                iterable = iter(o)
            except TypeError:
                pass
            else:
                return list(iterable)
            # Let the base class default method raise the TypeError
            return JSONEncoder.default(self, o)

`evennia.server.portal.webclient_ajax.``jsonify`(obj)

class `evennia.server.portal.webclient_ajax.``AjaxWebClient`

  Bases: `twisted.web.resource.Resource`

  An ajax/comet long-polling transport

  `isLeaf` = True
  `allowedMethods` = ('POST',)
  `__init__`()

    Initialize.

  `get_client_sessid`(request)

    Helper to get the client session id out of the request.

    Parameters

      request (Request) – Incoming request object.

    Returns

      csessid (int) – The client-session id.

  `get_browserstr`(request)

    Get browser-string out of the request.

    Parameters

      request (Request) – Incoming request object.

    Returns

      str – The browser name.

  `at_login`()

    Called when this session gets authenticated by the server.

  `lineSend`(csessid, data)

    This adds the data to the buffer and/or sends it to the client as soon as possible.

    Parameters

      - csessid (int) – Session id.

      - data (list) – A send structure [cmdname, [args], {kwargs}].

  `client_disconnect`(csessid)

    Disconnect session with given csessid.

    Parameters

      csessid (int) – Session id.

  `mode_init`(request)

    This is called by render_POST when the client requests an init mode operation (at startup)

    Parameters

      request (Request) – Incoming request.

  `mode_keepalive`(request)

    This is called by render_POST when the client is replying to the keepalive.

    Parameters

      request (Request) – Incoming request.

  `mode_input`(request)

    This is called by render_POST when the client is sending data to the server.

    Parameters

      request (Request) – Incoming request.

  `mode_receive`(request)

    This is called by render_POST when the client is telling us that it is ready to receive data as soon as it is available. This is the basis of a long-polling (comet) mechanism: the server will wait to reply until data is available.

    Parameters

      request (Request) – Incoming request.

  `mode_close`(request)

    This is called by render_POST when the client is signalling that it is about to be closed.

    Parameters

      request (Request) – Incoming request.

  `render_POST`(request)

    This function is what Twisted calls with POST requests coming in from the ajax client. The requests should be tagged with different modes depending on what needs to be done, such as initializing or sending/receving data through the request. It uses a long-polling mechanism to avoid sending data unless there is actual data available.

    Parameters

      request (Request) – Incoming request.

class `evennia.server.portal.webclient_ajax.``AjaxWebClientSession`(*args, **kwargs)

  Bases: `evennia.server.session.Session`

  This represents a session running in an AjaxWebclient.

  `__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.

  `disconnect`(reason='Server disconnected.')

    Disconnect from server.

    Parameters

      reason (str) – Motivation for the disconnect.

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

    Data User -> Evennia

    Keyword Arguments

      kwargs (any) – Incoming data.

  `data_out`(**kwargs)

    Data Evennia -> User

    Keyword Arguments

      kwargs (any) – Options to the protocol

  `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): Remove 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.