Skip to content

websock.js

Andrea Casaccia edited this page Aug 21, 2017 · 5 revisions

websock.js

The websock.js module is the client library for interacting with websockify. It has transparent support for binary data and has automatic fallback to the web-socket-js Flash Websocket shim/polyfill if the browser does not support WebSockets natively.

Module API

The Websock API is similar to the standard WebSocket object but there are some differences:

  • Callback routines are configured using the 'on' method rather than setting 'on*' attributes.
  • Instead of Strings, Websock sends and receives arrays of bytes (0-255).
  • The 'message' event only notifies that a new message has been received, decoded and placed on the end of the receive queue. The data is read using the 'rQ*' methods.

The methods and callbacks supported by WebSock are:

Methods name parameters description on (evt, handler) Add a callback handler for event name evt open (uri) Connect to the WebSockets server at uri close () Disconnect from the WebSockets server get_sQ () Returns a direct reference to the send queue get_rQ () Returns a direct reference to the receive queue get_rQi () Returns the receive queue index set_rQi () Set the receive queue index rQlen () Return the number of unread bytes on the receive queue rQpeek8 () Return the first byte of the receive queue without moving the index rQshift8 () Shift off and return a byte from the receive queue rQunshift8 (num) Unshift a byte onto the head of the receive queue rQshift16 () Shift off and return a 16-bit word byte from the receive queue rQshift32 () Shift off and return a 32-bit word byte from the receive queue rQshiftStr (len) Shift off and return len bytes converted to a String from the receive queue rQshiftBytes (len) Shift off and return len bytes as an Array of bytes from the receive queue rQslice (start, end) Return an Array of bytes from start to end relative to the receive queue index rQwait (msg, num, goback) If receive queue has num bytes left return false, otherwise return false flush () Try sending any pending data from the send queue send (array) Add an array of bytes to the send queue and try to flush send_string (str) Add a string (decode to bytes) to the send queue and try to flush

Callbacks name parameters description open () A connection was established to the WebSockets server message () A message has been received, decoded and added to the receive queue close (event) The connection has closed with event error (event) An error has occurred with event

Clone this wiki locally