Skip to main content

LensStudio:Network.TcpSocket

Class

TCP socket for use with "LensStudio:Network".TcpSocket.

Index

Example

import * as Network from "LensStudio:Network"

export default class TcpServerManager {
constructor() {
this.server = Network.TcpServer.create()
this.connections = []
this.sockets = []
this.onClientConnected = null
this.onClientDataReceived = null
this.onClientDisconnected = null
this.onClientSocketError = null
this.enableLogging = false

// Setup listeners
this.connections.push(
this.server.onConnect.connect(socket => {
//save sockets to the persistent array so they dont get garbage collected
this.sockets.push(socket)

if (this.enableLogging) {
console.log(`Incoming connection from ${socket.remoteAddress.address}:${socket.remoteAddress.port}`)
}

if (this.onClientConnected) {
this.onClientConnected(socket)
}

this.connections.push(
socket.onData.connect(data => {
if (this.enableLogging) {
console.log(`Received data from socket: ${data}`)
}

if (this.onClientDataReceived) {
this.onClientDataReceived(data, socket)
}
})
)

this.connections.push(
socket.onEnd.connect(() => {
if (this.enableLogging) {
console.log(`Socket connected to ${socket.remoteAddress.address}:${socket.remoteAddress.port} disconnected from the server.`)
}

if (this.onClientDisconnected) {
this.onClientDisconnected(socket)
}
})
)

this.connections.push(
socket.onError.connect(error => {
if (this.enableLogging) {
logger.logException(`Socket error: ${error}`)
}

if (this.onClientSocketError) {
this.onClientSocketError(error, socket)
}
})
)
})
)
}

start (address, port) {
const localhostAddr = new Network.Address()
localhostAddr.address = address
localhostAddr.port = port
try {
this.server.listen(localhostAddr)
console.log(`Server started at ${address}:${port}`)
} catch (e) {
console.log("Failed to start the server: " + e)
}
}

close (){
// Disconnect all the connections
this.connections.forEach(connection => connection.disconnect())
this.connections = []
// Close the server
this.server.close()
}
}

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


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


write()

write(data): number

Write to the socket.

Parameters

ParameterType
datastring | Uint8Array<ArrayBufferLike>

Returns

number