LensStudio:WebSocket.WebSocket
ClassClient-side WebSocket connection that can send and receive data over a WebSocket connection.
Index
Properties
Methods
Example
const WS = await import('LensStudio:WebSocket');
const Network = await import('LensStudio:Network');
const ws = WS.WebSocket.create();
this.connections.push(
ws.onConnect.connect(() => {
console.log('WebSocket connected');
ws.send('hello from plugin');
})
);
this.connections.push(
ws.onData.connect((data) => {
console.log(`Received data: ${data.toString()}`);
})
);
this.connections.push(
ws.onError.connect((code) => {
console.log(`WebSocket error: ${code}`);
})
);
const addr = new Network.Address();
addr.address = 'localhost';
addr.port = 8080;
ws.connect(addr);
Extends
Properties
localAddress
readonlylocalAddress:Address
The local address bound to this socket.
Inherited from
onConnect
readonlyonConnect:signal0<void>
Signal fired when the socket successfully establishes a connection.
Inherited from
onData
Signal fired when data is received on the socket, carrying the received buffer.
Inherited from
onEnd
readonlyonEnd:signal0<void>
Signal fired when the remote end signals the end of transmission.
Inherited from
onError
readonlyonError:signal1<number,void>
Signal fired when a socket error occurs.
Inherited from
remoteAddress
readonlyremoteAddress:Address
The remote address this socket is connected to.
Inherited from
Methods
close()
close():
void
Closes the socket connection gracefully.
Returns
void
Inherited from
connect()
connect(
address):void
Connects to a remote address using the WebSocket protocol.
Parameters
| Parameter | Type |
|---|---|
address | Address |
Returns
void
create()
staticcreate():WebSocket
Creates a new WebSocket instance.
Returns
WebSocket
destroy()
destroy():
void
Destroys the socket and releases all associated resources immediately.
Returns
void
Inherited from
getTypeName()
staticgetTypeName():string
Returns the type name of this class.
Returns
string
Overrides
getTypeName()
getTypeName():
string
Returns the name of this object's type.
Returns
string
Inherited from
isOfType()
isOfType(
type):boolean
Returns true if the object is of the specified type.
Parameters
| Parameter | Type |
|---|---|
type | string |
Returns
boolean
Inherited from
isSame()
isSame(
other):boolean
Returns true if this object refers to the same instance as the given object.
Parameters
| Parameter | Type |
|---|---|
other | ScriptObject |
Returns
boolean
Inherited from
send()
send(
data):number
Sends binary or text data over the WebSocket connection, returning the number of bytes sent.
Parameters
| Parameter | Type |
|---|---|
data | string | Uint8Array<ArrayBufferLike> |
Returns
number