Qore WebSocketClient Module Reference 1.0

- Main Page
- Related Pages
- Namespaces
- Classes

- Class List
- Class Index
- Class Members

All Classes Namespaces Functions Variables Groups Pages
Public Member Functions | List of all members
WebSocketClient::WebSocketClient Class Reference

the main websocket client class More...

## Public Member Functions
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
bool | isOpen ()
| returns True if the connection is currently open and active, False if not

## 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

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(("url": "ws://example.com:8080/path", "callback": \event()));

Parameters

  callback | the callhack 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