LensStudio:Network.TcpSocket
ClassTCP socket for use with "LensStudio:Network".TcpSocket.
Index
Properties
Methods
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
readonlylocalAddress:Address
The local address bound to this socket.
Inherited from
onConnect
readonlyonConnect:signal0<void>
Signal fired when the socket successfully establishes a connection.
Inherited from
onData
Signal fired when data is received on the socket, carrying the received buffer.
Inherited from
onEnd
readonlyonEnd:signal0<void>
Signal fired when the remote end signals the end of transmission.
Inherited from
onError
readonlyonError:signal1<number,void>
Signal fired when a socket error occurs.
Inherited from
remoteAddress
readonlyremoteAddress:Address
The remote address this socket is connected to.
Inherited from
Methods
close()
close():
void
Closes the socket connection gracefully.
Returns
void
Inherited from
destroy()
destroy():
void
Destroys the socket and releases all associated resources immediately.
Returns
void
Inherited from
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
write()
write(
data):number
Write to the socket.
Parameters
| Parameter | Type |
|---|---|
data | string | Uint8Array<ArrayBufferLike> |
Returns
number