User Tools

Site Tools


en:code:data:tcpserver

TcpServer

This object allows you to create a TCP server, allowing you to listen to incoming connections.

Functions

TcpServer

Constructor function.

Syntax

var myObject = new TcpServer(events);

Arguments

  1. events - (object) the events that can be called
    • onNewConnection - (onNewConnection) called when a client is trying to establish a connection

Example

var myObject = new TcpServer();
var myObject = new TcpServer({
	onNewConnection: function()
	{
		Console.print("New client!");
	}
});

Methods

listen

Start listening for clients.

Syntax

myObject.listen(address, port);
myObject.listen(address);
myObject.listen();

Arguments

  1. address - (string) the address where to listen (default: all network interfaces)
  2. port - (integer) the port number (default: choose a port automatically)

Returns

  • (TcpServer) this TcpServer

Exceptions

  • (ListenError) unable to start listening

waitForNewConnection

Freezes the execution until a new connection arrives or waitTime time elapsed.

Syntax

myObject.waitForNewConnection(waitTime);
myObject.waitForNewConnection();

Arguments

  1. waitTime - (integer) the time to wait (milliseconds, default: 30000)

Returns

  • (TcpServer) this TcpServer

Exceptions

  • (WaitForNewConnectionError) waiting for new connection failed

nextPendingConnection

Returns the next pending connection as a Tcp object that can be used to read and write to the client. You should store this object if you want to write to this client later.

Syntax

myObject.nextPendingConnection();

Returns

  • (Tcp) the new client as a Tcp object

Exceptions

  • (NoPendingConnectionError) there is no pending connection

address

Returns the ip address on which this server is listening.

Syntax

myObject.address();

Returns

  • (string) the ip address on which this server is listening

port

Returns the port used by this server.

Syntax

myObject.port();

Returns

  • (integer) the port used by this server

Events

onNewConnection

Called when a new client has connected.

Syntax

myObject.onNewConnection = function() {};

Example

myObject.onNewConnection = function()
{
	var client = myObject.nextPendingConnection();
 
	// do something with the client...
};
en/code/data/tcpserver.txt · Last modified: 2016/10/09 11:10 (external edit)