LensStudio:Network.TcpServer
ClassA 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
readonlyaddress:string
The address string the server is currently bound to.
Inherited from
onConnect
readonlyonConnect:signal1<BaseSocket,void>
Signal fired when a new client socket connects to the server.
Inherited from
onError
readonlyonError:signal1<number,void>
Signal fired when a server error occurs, passing an error code.
Inherited from
port
readonlyport:number
Port number the server is currently bound to.
Inherited from
Methods
close()
close():
void
Closes the server and stops accepting new connections.
Returns
void
Inherited from
create()
staticcreate():TcpServer
Create a TCP Server.
Returns
TcpServer
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
listen()
listen(
address):boolean
Starts listening for incoming connections on the specified address; returns true if successful.
Parameters
| Parameter | Type |
|---|---|
address | Address |
Returns
boolean