Skip to main content

LensStudio:WebSocket.WebSocketServer

Class

Server that accepts incoming WebSocket connections and manages connected client sockets.

Index

Example

const WS = await import('LensStudio:WebSocket');
const Network = await import('LensStudio:Network');
const server = WS.WebSocketServer.create();
this.connections.push(
server.onConnect.connect((clientSocket) => {
console.log(`Client connected from ${clientSocket.remoteAddress.address}`);
clientSocket.onData.connect((data) => {
console.log(`Server received: ${data.toString()}`);
});
})
);
const addr = new Network.Address();
addr.address = '127.0.0.1';
addr.port = 9090;
const listening = server.listen(addr);
console.log(`WebSocketServer listening on ${server.address}: ${listening}`);

Extends

Properties

address

readonly address: string

The address string the server is currently bound to.

Inherited from

BaseServer.address


onConnect

readonly onConnect: signal1<BaseSocket, void>

Signal fired when a new client socket connects to the server.

Inherited from

BaseServer.onConnect


onError

readonly onError: signal1<number, void>

Signal fired when a server error occurs, passing an error code.

Inherited from

BaseServer.onError


port

readonly port: number

Port number the server is currently bound to.

Inherited from

BaseServer.port

Methods

close()

close(): void

Closes the server and stops accepting new connections.

Returns

void

Inherited from

BaseServer.close


create()

static create(): WebSocketServer

Creates and returns a new WebSocketServer instance.

Returns

WebSocketServer


getTypeName()

static getTypeName(): string

Returns the type name of this class.

Returns

string

Overrides

BaseServer.getTypeName


getTypeName()

getTypeName(): string

Returns the name of this object's type.

Returns

string

Inherited from

BaseServer.getTypeName


isOfType()

isOfType(type): boolean

Returns true if the object is of the specified type.

Parameters

ParameterType
typestring

Returns

boolean

Inherited from

BaseServer.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

BaseServer.isSame


listen()

listen(address): boolean

Starts listening for incoming connections on the specified address; returns true if successful.

Parameters

ParameterType
addressAddress

Returns

boolean

Inherited from

BaseServer.listen