获取会话的连接和断开事件
关键字: 连接事件, 断开事件, OnSessionStarted, OnSessionClosed, NewSessionConnected, SessionClosed
AppSession 的虚方法 OnSessionStarted() 和 OnSessionClosed(CloseReason reason)
你可以覆盖基类的虚方法 OnSessionStarted() 和 OnSessionClosed(CloseReason reason) 用于在会话连接和断开时执行一些逻辑操作:
public class TelnetSession : AppSession<TelnetSession>
{
protected override void OnSessionStarted()
{
this.Send("Welcome to SuperSocket Telnet Server");
//add your business operations
}
protected override void OnSessionClosed(CloseReason reason)
{
//add your business operations
}
}
AppServer 的两个事件: NewSessionConnected 和 SessionClosed
订阅事件:
appServer.NewSessionConnected += new SessionHandler<AppSession>(appServer_NewSessionConnected);
appServer.SessionClosed += new SessionHandler<AppSession, CloseReason>(appServer_SessionClosed);
定义事件处理方法:
static void appServer_SessionClosed(AppSession session, CloseReason reason)
{
Console.WriteLine("A session is closed for {0}.", reason);
}
static void appServer_NewSessionConnected(AppSession session)
{
session.Send("Welcome to SuperSocket Telnet Server");
}
当前内容版权归 SuperSocket 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 SuperSocket .