Lifetime events
There are several lifetime events on controller level.
OnOpened
Will be triggered when a new instance of a controller is created. That is when the clients sends the init
command or communicates on a controller that is not yet initialized.
OnClosed
Will be triggered when the socket is closed or when the client sends the close
command
OnReopened
Will be triggered when the client connect and pass in a PersistentId
with the handshake. It is up to the client implementation to pass in the previously used PersistenId
.
Sample code
We can override all of the lifetime events in the controller to just log information when each event occur.
using System.Threading.Tasks;
using XSockets.Core.Common.Socket.Event.Interface;
using XSockets.Core.Common.Utility.Logging;
using XSockets.Core.XSocket;
using XSockets.Core.XSocket.Helpers;
using XSockets.Plugin.Framework;
namespace XSocketsTutorial
{
public class Bar : XSocketController
{
public override async Task OnMessage(IMessage message)
{
Composable.GetExport<IXLogger>().Information("OnMessage {@m}", message);
await this.InvokeToAll(message);
}
public override async Task OnOpened()
{
Composable.GetExport<IXLogger>().Information("OnOpened {p}", this.PersistentId);
await base.OnOpened();
}
public override async Task OnClosed()
{
Composable.GetExport<IXLogger>().Information("OnClosed {p}", this.PersistentId);
await base.OnClosed();
}
public override async Task OnReopened()
{
Composable.GetExport<IXLogger>().Information("OnReopened {p}", this.PersistentId);
await base.OnReopened();
}
}
}
Now we can use Putty (again) to trigger these events.
OnOpened
By sending the init
command to the bar
controller the method OnOpened
was invoked and we can see the output in the log.
OnClosed
By sending the close
command to the bar
controller the method OnClosed
was invoked and we can see the output in the log.
OnReopened
To trigger the OnReopened
method we have to pass in the PersistentId with the handshake. So I copied the PersistentId we got from the connections and will pass it in with the new handshake.
I will send the handshake like this.
PuttyProtocol?PersistentId=d2b6452b-2e59-4e70-885c-227568d32b61
When the init
command is sent it will OnOpened
, but also OnReopened