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");
        //添加你的业务操作
    }
    protected override void OnSessionClosed(CloseReason reason)
    {
        //添加你的业务操作
    }
}

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");
}

 

相关文章: