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)[source]

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

Implements the server-side of the Websocket connection.

nonce = 0
__init__(*args, **kwargs)[source]

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


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


csession (ClientSession)

This is a django-specific internal representation

of the browser session.


This is called when the WebSocket connection is fully established.


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


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

onClose(wasClean, code=None, reason=None)[source]

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

  • 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)[source]

Callback fired when a complete WebSocket message was received.

  • payload (bytes) – The WebSocket message received.

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


Send data to client.


line (str) – Text to send.


Data User > Evennia.

  • text (str) – Incoming text.

  • kwargs (any) – Options from protocol.


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)[source]

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


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)[source]
send_default(cmdname, *args, **kwargs)[source]

Data Evennia -> User.

  • 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.