Home Forums Support Connection Establish/close handler example Reply To: Connection Establish/close handler example

#589
MarcF
Keymaster

Heya IZZO, welcome to our forums and many thanks for your interest in our network library.

Ok, so the general idea of establish and close handlers is that you can trigger some code when a connection is established or closed.

If we define the following two methods:

static void ClientConnected(Connection connection)
{
    Console.WriteLine("A new client connected - " + connection.ToString());
}

static void ClientDisconnected(Connection connection)
{
    Console.WriteLine("A client has disconnected - " + connection.ToString());
}

all they do is write some text to a console window. You could perform whatever you want.

We can then tell NetworkComms what to do with these, either on a global level, so that the methods are executed for all connections:

NetworkComms.AppendGlobalConnectionEstablishHandler(ClientConnected);
NetworkComms.AppendGlobalConnectionCloseHandler(ClientDisconnected);

or on a connection specific level:

Connection newConnection = TCPConnection.GetConnection(new ConnectionInfo("127.0.0.1", 10000));
newConnection.AppendShutdownHandler(ClientDisconnected);

There is no connection specific establish handler method because it would be superfluous.

Hope that’s what you were after, if not please let me know which bit you are unsure about.

Marc