Skip to main content

LensStudio:Network.TcpServer

Class

A class to accept TCP connetions.

A class to accept TCP connetions. Useful for receiving streaming data. It's also able to send back responses.

Index

Example

// TcpServer extends BaseServer and accepts incoming TCP connections.
// Create one via the static create() method, configure an Address, then listen.
const { TcpServer, Address } = await import('LensStudio:Network');

const server = TcpServer.create();
console.log('Created TcpServer instance');

// Configure the address to listen on
const addr = new Address();
addr.address = '127.0.0.1';
addr.port = 0; // OS picks an available port
console.log('Configured address:', addr.address, 'port:', addr.port);

// Start listening
const listening = server.listen(addr);
console.log('Server listening:', listening);
console.log('Bound address:', server.address);

// onConnect fires when a client connects
this.connections.push(
server.onConnect.connect((socket: import('LensStudio:Network').BaseSocket) => {
console.log('Client connected');
})
);

// onError fires on server errors
this.connections.push(
server.onError.connect((errorCode: number) => {
console.log('Server error, code:', errorCode);
})
);

// Clean up by closing the server
server.close();
console.log('TcpServer closed');

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(): TcpServer

Create a TCP Server.

Returns

TcpServer


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