Skip to main content

LensStudio:WebSocket.WebSocket

Class

Client-side WebSocket connection that can send and receive data over a WebSocket connection.

Index

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

readonly localAddress: Address

The local address bound to this socket.

Inherited from

BaseSocket.localAddress


onConnect

readonly onConnect: signal0<void>

Signal fired when the socket successfully establishes a connection.

Inherited from

BaseSocket.onConnect


onData

readonly onData: signal1<Buffer, void>

Signal fired when data is received on the socket, carrying the received buffer.

Inherited from

BaseSocket.onData


onEnd

readonly onEnd: signal0<void>

Signal fired when the remote end signals the end of transmission.

Inherited from

BaseSocket.onEnd


onError

readonly onError: signal1<number, void>

Signal fired when a socket error occurs.

Inherited from

BaseSocket.onError


remoteAddress

readonly remoteAddress: Address

The remote address this socket is connected to.

Inherited from

BaseSocket.remoteAddress

Methods

close()

close(): void

Closes the socket connection gracefully.

Returns

void

Inherited from

BaseSocket.close


connect()

connect(address): void

Connects to a remote address using the WebSocket protocol.

Parameters

ParameterType
addressAddress

Returns

void


create()

static create(): WebSocket

Creates a new WebSocket instance.

Returns

WebSocket


destroy()

destroy(): void

Destroys the socket and releases all associated resources immediately.

Returns

void

Inherited from

BaseSocket.destroy


getTypeName()

static getTypeName(): string

Returns the type name of this class.

Returns

string

Overrides

BaseSocket.getTypeName


getTypeName()

getTypeName(): string

Returns the name of this object's type.

Returns

string

Inherited from

BaseSocket.getTypeName


isOfType()

isOfType(type): boolean

Returns true if the object is of the specified type.

Parameters

ParameterType
typestring

Returns

boolean

Inherited from

BaseSocket.isOfType


isSame()

isSame(other): boolean

Returns true if this object refers to the same instance as the given object.

Parameters

ParameterType
otherScriptObject

Returns

boolean

Inherited from

BaseSocket.isSame


send()

send(data): number

Sends binary or text data over the WebSocket connection, returning the number of bytes sent.

Parameters

ParameterType
datastring | Uint8Array<ArrayBufferLike>

Returns

number