// 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}`) }
start (address, port) { constlocalhostAddr = newNetwork.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() } }
TCP socket for use with "LensStudio:Network".TcpSocket.
Example