2013年3月16日

SuperSocket的研究(8)—— 获取连接的建立和关闭事件

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

没有评论:

发表评论