1.2.15. web_sockets

1.2.15.1. Classes

class Event(event_id, event_type, sources)[source]

Bases: object

An object representing an event which occurred on the server in a way that is ready to be published to client subscribers.

__init__(event_id, event_type, sources)[source]

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

event_id[source]

The unique string identifier of this event.

event_type[source]

The unique string identifier of the type of this event.

sources[source]

The source objects which are associated with this event.

class EventSocket(handler, manager)[source]

Bases: advancedhttpserver.WebSocketHandler

A socket through which server events are published to subscribers. This socket will automatically add and remove itself from the manager that is initialized with.

__init__(handler, manager)[source]
Parameters:
is_subscribed(event_id, event_type)[source]

Check if the client is currently subscribed to the specified server event.

Parameters:
  • event_id (str) – The identifier of the event to subscribe to.
  • event_type (str) – A sub-type for the corresponding event.
Returns:

Whether or not the client is subscribed to the event.

Return type:

bool

on_closed()[source]

A method that can be over ridden and is called after the web socket is closed.

publish(event)[source]

Publish the event by sending the relevant information to the client. If the client has not requested to receive the information through a subscription, then no data will be sent.

Parameters:event (Event) – The object representing the data to be published.
subscribe(event_id, event_types=None, attributes=None)[source]

Subscribe the client to the specified event published by the server. When the event is published the specified attributes of it and it’s corresponding id and type information will be sent to the client.

Parameters:
  • event_id (str) – The identifier of the event to subscribe to.
  • event_types (list) – A list of sub-types for the corresponding event.
  • attributes (list) – A list of attributes of the event object to be sent to the client.
unsubscribe(event_id, event_types=None, attributes=None)[source]

Unsubscribe from an event published by the server that the client previously subscribed to.

Parameters:
  • event_id (str) – The identifier of the event to subscribe to.
  • event_types (list) – A list of sub-types for the corresponding event.
  • attributes (list) – A list of attributes of the event object to be sent to the client.
class WebSocketsManager(config, job_manager)[source]

Bases: object

An object used to manage connected web sockets.

__init__(config, job_manager)[source]
Parameters:
  • config (smoke_zephyr.configuration.Configuration) – Configuration to retrieve settings from.
  • job_manager (smoke_zephyr.job.JobManager) – A job manager instance that can be used to schedule tasks.
add(web_socket)[source]

Add a connected web socket to the manager.

Parameters:web_socket (advancedhttpserver.WebSocketHandler) – The connected web socket.
dispatch(handler)[source]

A method that is suitable for use as a web_socket_handler.

Parameters:handler (KingPhisherRequestHandler) – The current request handler instance.
logger = <Logger KingPhisher.Server.WebSocketManager (WARNING)>[source]
ping_all()[source]

Ping all of the connected web sockets to ensure they stay alive. This method is automatically executed periodically through a job added when the manager is initialized.

remove(web_socket)[source]

Remove a connected web socket from those that are currently being managed. If the web socket is not currently being managed, no changes are made.

Parameters:web_socket (advancedhttpserver.WebSocketHandler) – The connected web socket.
stop()[source]

Shutdown the manager and clean up the resources it has allocated.