Skip to main content

LensStudio:Network.BaseSocket

Class

Base class for network sockets, providing common send and event handling functionality.

Index

Example

// BaseSocket is the base class for network sockets. Use WebSocket as a
// concrete subclass to demonstrate connect(), close(), onConnect, onData, onError.
const ws = WebSocket.create();
this.socket = ws;

this.connections.push(
ws.onConnect.connect(() => {
console.log('WebSocket connected');
ws.send('hello from plugin');
})
);
this.connections.push(
ws.onData.connect((buffer: Editor.Buffer) => {
console.log('Received data:', buffer.toString());
})
);
this.connections.push(
ws.onEnd.connect(() => {
console.log('WebSocket connection ended');
})
);
this.connections.push(
ws.onError.connect((code: number) => {
console.log('WebSocket error code:', code);
})
);

const addr = new Address();
addr.address = 'localhost';
addr.port = 8080;
ws.connect(addr);
console.log('WebSocket connecting to', addr.address + ':' + addr.port);

Extends

Extended by

Properties

localAddress

readonly localAddress: Address

The local address bound to this socket.


onConnect

readonly onConnect: signal0<void>

Signal fired when the socket successfully establishes a connection.


onData

readonly onData: signal1<Buffer, void>

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


onEnd

readonly onEnd: signal0<void>

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


onError

readonly onError: signal1<number, void>

Signal fired when a socket error occurs.


remoteAddress

readonly remoteAddress: Address

The remote address this socket is connected to.

Methods

close()

close(): void

Closes the socket connection gracefully.

Returns

void


destroy()

destroy(): void

Destroys the socket and releases all associated resources immediately.

Returns

void


getTypeName()

static getTypeName(): string

Returns the type name of this class.

Returns

string

Overrides

ScriptObject.getTypeName


getTypeName()

getTypeName(): string

Returns the name of this object's type.

Returns

string

Inherited from

ScriptObject.getTypeName


isOfType()

isOfType(type): boolean

Returns true if the object is of the specified type.

Parameters

ParameterType
typestring

Returns

boolean

Inherited from

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

ScriptObject.isSame