All Classes Namespaces Functions Variables Groups Pages
WebSocketClient::WebSocketClient Class Reference

the main websocket client class More...

## Public Member Functions

| clearStats ()
| Clears performance statistics. More...
nothing | clearWarningQueue ()
| Removes any warning Queue object from the Socket. More...
hash | connect (*hash opts, *reference info)
| connects to the websocket server More...
| constructor (code callback, hash opts)
| creates the object and optionally sets logging targets More...
| disconnect (int cmd=WSCC_GoingAway)
| disconnect with the given close code
hash | getUsageInfo ()
| Returns performance statistics for the socket. More...
bool | isOpen ()
| returns True if the connection is currently open and active, False if not
nothing | setWarningQueue (int warning_ms, int warning_bs, Queue queue, any arg, timeout min_ms=1s)
| Sets a Queue object to receive socket warnings. More...

## Detailed Description

the main websocket client class

To use this class, create a WebSocketClient object and the call WebSocketClient::connect().

The WebSocketClient::connect() method starts a background thread to receive messages, which are then posted to the callback provided in the WebSocketClient::connect() method call.

To stop listening for web socket events, call WebSocketClient::disconnect().

## Member Function Documentation

WebSocketClient::WebSocketClient::clearStats | ( |  | ) |

Clears performance statistics.

Example:
  $ws.clearStats();

Since
  WebSocketClient 1.1

See Also
  WebSocketClient::getUsageInfo()

nothing WebSocketClient::WebSocketClient::clearWarningQueue | ( |  | ) |

Removes any warning Queue object from the Socket.

Example:
  $ws.clearWarningQueue();

See Also
  WebSocketClient::setWarningQueue()

Since
  WebSocketClient 1.1

hash WebSocketClient::WebSocketClient::connect | ( | *hash | opts,
|  | *reference | info
| ) |  |

connects to the websocket server

Example:
  sub event(*data $msg) {
  if (!$msg)
  printf("connection closed\n");
  else
  printf("%s msg: %y\n", now_us().format("YYYY-MM-DD HH:mm:SS.xx"), $msg);
  }
  my WebSocketClient $ws(("url": "ws://example.com:8080/path", "callback": \event()));
  $ws.connect();

This method starts a background thread to receive messages, which are then posted to the callback closure or call reference given as an argument. If the server disconnects the web socket connection, the callback will be called with no argument (ie NOTHING). In this case the event thread also terminates and the WebSocketClient object will be in a disconnected state.

To stop listening for web socket events, call WebSocketClient::disconnect().

If this method is called while a connection is already in progress, then the existing connection is first implicitly disconnected with close code WSCC_GoingAway.

Parameters

  opts

  a hash with the following keys:
  - `hdr:` (optional) a hash giving header values for the connection request to the web socket server

  info | a reference to a hash which will be set to information about the call setup

Returns
  a hash with information about the HTTP response from the webn socket server corresponding to the return value of Qore::Socket::readHTTPHeader()

Exceptions

  WEBSOCKET-ERROR | the option hash is missing either the 'url' or 'callback' keys or type error in the option hash

WebSocketClient::WebSocketClient::constructor | ( | code | callback,
|  | hash | opts
| ) |  |

creates the object and optionally sets logging targets

Example:
  sub event(*data $msg) {
  if (!$msg)
  printf("connection closed\n");
  else
  printf("%s msg: %y\n", now_us().format("YYYY-MM-DD HH:mm:SS.xx"), $msg);
  }
  my WebSocketClient $ws(\event(), ("url": "ws://example.com:8080/path"));

Parameters

  callback | the callback closure or call reference for received messages

  opts

  an option hash for the Qore::HTTPClient class plus the following keys:
  - `log:` an optional closure or call reference for informational logging
  - `errlog:` an optional closure or call reference for error logging
  - `debuglog:` an optional closure or call reference for logging debugging/technical messages about the web socket connection

Exceptions

  WEBSOCKET-ERROR | unknown scheme, missing 'url' key in option hash

hash WebSocketClient::WebSocketClient::getUsageInfo | ( |  | ) |

Returns performance statistics for the socket.

Example:
  my hash $h = $ws.getUsageInfo();

Returns
  a hash with the following keys:
  - `"bytes_sent"`: an integer giving the total amount of bytes sent
  - `"bytes_recv"`: an integer giving the total amount of bytes received
  - `"us_sent"`: an integer giving the total number of microseconds spent sending data
  - `"us_recv"`: an integer giving the total number of microseconds spent receiving data
  - `"arg"`: (only if warning values have been set with WebSocketClient::setWarningQueue()) the optional argument for warning hashes
  - `"timeout"`: (only if warning values have been set with WebSocketClient::setWarningQueue()) the warning timeout in microseconds
  - `"min_throughput"`: (only if warning values have been set with WebSocketClient::setWarningQueue()) the minimum warning throughput in bytes/sec

Since
  WebSocketClient 1.1

See Also
  WebSocketClient::clearStats()

nothing WebSocketClient::WebSocketClient::setWarningQueue | ( | int | warning_ms,
|  | int | warning_bs,
|  | Queue | queue,
|  | any | arg,
|  | timeout | min_ms = `1s`
| ) |  |

Sets a Queue object to receive socket warnings.

Example:
  $ws.setWarningQueue(5000, 5000, $queue, "socket-1");

Parameters

  warning_ms

  the threshold in milliseconds for individual socket actions (send, receive, connect), if exceeded, a socket warning is placed on the warning queue with the following keys:
  - `"type"`: a string with the constant value `"SOCKET-OPERATION-WARNING"`
  - `"operation"`: a string giving the operation that caused the warning (ex: `"connect"`)
  - `"us"`: an integer giving the number of microseconds for the operation
  - `"timeout"`: an integer giving the warning threshold in microseconds
  - `"arg"`: if any `"arg"` argument is passed to the WebSocketClient::setWarningQueue() method, it will be included in the warning hash here

  warning_bs

  value in bytes per second; if any call has performance below this threshold, a socket warning is placed on the warning queue with the following keys:
  - `"type"`: a string with the constant value `"SOCKET-THROUGHPUT-WARNING"`
  - `"dir"`: either `"send"` or `"recv"` depending on the direction of the data flow
  - `"bytes"`: the amount of bytes sent
  - `"us"`: an integer giving the number of microseconds for the operation
  - `"bytes_sec"`: a float giving the transfer speed in bytes per second
  - `"threshold"`: an integer giving the warning threshold in bytes per second
  - `"arg"`: if any `"arg"` argument is passed to the WebSocketClient::setWarningQueue() method, it will be included in the warning hash here

  queue | the Queue object to receive warning events
  arg | an optional argument to be placed in the `"arg"` key in each warning hash (could be used to identify the socket for example)
  min_ms | the minimum transfer time with a resolution of milliseconds for a transfer to be eligible for triggering a warning; transfers that take less than this period of time are not eligible for raising a warning

Exceptions

  QUEUE-ERROR | the Queue passed has a maximum size set
  SOCKET-SETWARNINGQUEUE-ERROR | at least one of warning_ms and warning_bs must be > 0

See Also
  WebSocketClient::clearWarningQueue()

Since
  WebSocketClient 1.1