Access AppServer in AppSession and Commands

Accessing AppServer in AppSession and Commands is very convinient.

The code below give you a guide:

 

public class YourSession : AppSession<YourSession>
    {
        public override void StartSession()
        {
            SendResponse("Welcome to " + AppServer.Name);   
        }

        public override void HandleExceptionalError(Exception e)
        {
            
        }
    }
public class STOP : StringCommandBase<EchoSession>
    {
        #region CommandBase<EchoSession> Members

        public override void ExecuteCommand(EchoSession session, StringCommandInfo commandInfo)
        {
            session.AppServer.Stop();
        }

        #endregion
    }

 

 

But it only allow you to access basic AppServer class's attributes and methods, but you can not use this AppServer as your own defined AppServer.

To work around it, you can override the attribute AppServer of AppSession using the keyword "new":

public class BroadcastSession : AppSession<BroadcastSession>
    {
        public string DeviceNumber { get; set; }

        public new BroadcastServer AppServer
        {
            get { return (BroadcastServer)base.AppServer; }
        }

        public override void HandleExceptionalError(Exception e)
        {
            
        }
    }

Then you can use the methods and attributes in your defined own AppServer in your AppSession and Commands directly:

public class BROA : StringCommandBase<BroadcastSession>
    {
        public override void ExecuteCommand(BroadcastSession session, StringCommandInfo commandData)
        {
            string message = commandData.Data;
            session.AppServer.BroadcastMessage(session, message);
            session.SendResponse("101 message broadcasted");
        }
    }

Last edited Mar 19, 2011 at 4:29 AM by kerryjiang, version 4

Comments

No comments yet.