Abstract
Wearable Only
Controls how binary data is received over the connection. Currently only blob
is supported. The arraybuffer
type is not yet supported.
// Create WebSocket connection.
webSocket = script.remoteServiceModule.createWebSocket("wss://<some-url>");
socket.binaryType = "blob";
// Listen for messages
socket.addEventListener("message", (event) => {
if (event.data instanceof Blob) {
// Binary frame, can be retrieved as either Uint8Array or string
let bytes = await event.data.bytes();
let text = await event.data.text();
} else {
// Text frame
let text = event.data;
}
});
Set a listener for the close
event. The event passed is WebSocketCloseEvent. Equivalent to addEventListener("close", ...)
. This listener will be run in addition to any listeners added via addEventListener
.
websocket.onclose = (event) => {
print("The connection has been closed.");
};
Set a listener for the error
event. The event passed is WebSocketErrorEvent. Equivalent to addEventListener("error", ...)
. This listener will be run in addition to any listeners added via addEventListener
.
websocket.onerror = (event) => {
print("The connection has been closed due to an error: " + error);
};
Set a listener for the message
event. The event passed is WebSocketMessageEvent. Equivalent to addEventListener("message", ...)
. This listener will be run in addition to any listeners added via addEventListener
.
websocket.onmessage = async (event) => {
if (event.data instanceof Blob) {
// Binary frame, can be retrieved as either Uint8Array or string
let bytes = await event.data.bytes();
let text = await event.data.text();
print("Received binary message, printing as text: " + text);
} else {
// Text frame
let text = event.data;
print("Received text message: " + text);
}
};
Set a listener for the open
event. The event passed is WebSocketEvent. Equivalent to addEventListener("open", ...)
. This listener will be run in addition to any listeners added via addEventListener
.
websocket.onopen = (event) => {
print("The connection has been opened successfully.");
};
Readonly
readyReturns the current state of the WebSocket connection.
CONNECTING (0)
Socket has been created. The connection is not yet open.
OPEN (1)
The connection is open and ready to communicate.
CLOSING (2)
The connection is in the process of closing.
CLOSED (3)
The connection is closed or couldn't be opened.
Readonly
urlReturns the url to which the WebSocket is connecting/connected.
Adds a listener for a WebSocket event. Supported event types are:
close
Fired when a connection with a WebSocket is closed. Also available via the onclose property.
error
Fired when a connection with a WebSocket has been closed because of an error, such as when some data couldn't be sent. Also available via the onerror property.
message
Fired when data is received through a WebSocket. Also available via the onmessage property.
open
Fired when a connection with a WebSocket is opened. Also available via the onopen property.
Returns true if this object is the same as other
. Useful for checking if two references point to the same thing.
Enqueues the given data to be transmitted to the server over the WebSocket connection. If the data can't be sent (for example, because it needs to be buffered but the buffer is full), the socket is closed automatically.
Text or binary data can be sent via this method. To send text data pass a string
into the send function. To send binary, pass a Uint8Array
.
Text example:
webSocket.send("Hello World");
Binary example:
// This sequence is `Hello World` in Uint8
const uint8Array = new Uint8Array([
72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100
]);
webSocket.send(uint8Array);
WebSocket provides an API for managing a WebSocket connection to a server, as well as for sending and receiving data on the connection.
Example