This accessor sends and/or receives messages from a web socket at
the specified host and port.
provide full-duplex communication channels over a single TCP/IP connection.
initialize(), it begins connecting to the web socket server.
Once the connection is established, a
true boolean is sent to
If connection is not established immediately, the accessor will attempt to
reconnect numberOfRetries times at an interval of reconnectInterval.
Whenever an input is received on the
input, the message is sent to the socket. If the socket is not yet open,
this accessor will, by default, queue the message to send when the socket opens,
discardMessagesBeforeOpen parameter is true, in which case,
input messages that are received before the socket is opened will be
discarded. If messages are queued and
throttleFactor is non-zero, then
whenever a message is queued to be later sent, the accessor's input handler will stall
by a number of milliseconds given by the queue size times the throttleFactor.
The longer the queue, the longer the stall. Note that this will likely block
the host from executing, so this feature should be used with caution.
Whenever a message is received from the socket, that message is
produced on the
'received' output. Note that the message may actually be sent
over multiple 'frames', but the frames will be aggregated and produced as one
wrapup() is invoked, this accessor closes the
If the connection is dropped midway, the swarmlet may monitor the 'connected' output for a value 'false' and attempt a reconnection by providing either a port or server input.
The default type for both sending and receiving is 'application/json', which allows sending and receiving anything that has a string representation in JSON. The types supported by this implementation include at least:
- application/json: The this.send() function uses JSON.stringify() and sends the result with a UTF-8 encoding. An incoming byte stream will be parsed as JSON, and if the parsing fails, will be provided as a string interpretation of the byte stream.
- text/*: Any text type is sent as a string encoded in UTF-8.
image/x: Where x is one of json, png, gif, and more. In this case, the data passed to this.send() is assumed to be an image, as encoded on the host, and the image will be encoded as a byte stream in the specified format before sending. A received byte stream will be decoded as an image, if possible.
When a model with an instance of this accessor stops executing, there are two mechanisms by which data in transit can be lost. In both cases, warning messages or error messages will be issued to the host to be displayed or otherwise handled as the host sees fit.
First, there might be queued messages that were received on
toSendbut have not yet been sent, either because the socket has not yet been opened or because it was closed from the other side.
Second, a message might be received from the server after shutdown has commenced. In particular, received messages are handled asynchronously by a handler function that can be invoked at any time, and that handler might be invoked after it is no longer possible for this accessor to produce outputs (it has entered its wrapup phase of execution).
The server might similarly lose messages by the same two mechanisms occurring on the server side. In that case, messages will presumably be displayed on the server side.
Accessors that extend this one can override the
toSendInputHandlerfunction to customize what is sent. See
RosPublisher.jsfor an example.
This accessor requires the 'webSocket' module.
||string||The IP address or domain name of server. Defaults to 'localhost'.|
||int||The port on the server to connect to. Defaults to -1, which means wait for a non-negative input before connecting.|
||The data to be sent over the socket.|
||boolean||Output `true` on connected and `false` on disconnected.|
||The data received from the web socket server.|
||string||The MIME type for incoming messages, which defaults to 'application/json'.|
||string||The MIME type for outgoing messages, which defaults to 'application/json'.|
||int||The time in milliseconds to wait before giving up on a connection (default is 1000).|
||int||The number of times to retry if a connection fails. Defaults to 5.|
||int||The time between retries in milliseconds. Defaults to 500.|
||boolean||Whether to trust any server certificate. This defaults to false. Setting it to true means that if sslTls is set to true, then any certificate provided by the server will be trusted.|
||string||If sslTls is set to true and trustAll is set to false, then this option needs to specify the fully qualified filename for the file that stores the certificate of a certificate authority (CA) that this client will use to verify server certificates. This path can be any of those understood by the Ptolemy host, e.g. paths beginning with $CLASSPATH/. FIXME: Need to be a list of paths for certificates rather than a single path.|
||boolean||Whether SSL/TLS is enabled. This defaults to false.|
||boolean||If true, then any messages received on `toSend` before the socket is open will be discarded. This defaults to false.|
||int||If non-zero, specifies a time (in milliseconds) to stall when a message is queued because the socket is not yet open. The time of the stall will be the queue size (after adding the message) times the throttleFactor. This defaults to 100. Making it non-zero causes the input handler to take time if there are pending unsent messages.|
Initiate a connection to the server using the current parameter values, set up handlers for for establishment of the connection, incoming data, errors, and closing from the server, and set up a handler for inputs on the toSend() input port.
Set up input handlers, and if the current value of the 'port' input is non-negative, initiate a connection to the server using the current parameter values, and set up handlers for for establishment of the connection, incoming data, errors, and closing from the server.
Send false to 'connected' output. This will be called if either side closes the connection.
Send the message received from web socket to the 'received' output.
Executes once web socket establishes a connection. Sets 'connected' output to true.
Sends JSON data to the web socket.
Set up the accessor by defining the parameters, inputs, and outputs.
Handles input on 'toSend'.
Close the web socket connection.