What is Appsession?
Appsession represents a logical connection to a client, and a connection-based operation should be set within that class. You can use instances of the class to send data to the client, receive data sent by the client, or close the connection. You can also save the data that is associated with the client.
What is AppServer?
AppServer represents a server instance that listens for client connections and hosts TCP connections. Ideally, we can get any client connection you want through the AppServer instance, and the server-level operations and logic should be defined in this class.
The first step: Create your Appsession
Why create Appsession, I think, the Quick start system, the following three points is enough for you to use.
- It is necessary to re-implement session creation, receive unknown protocol data, handle exceptions and other overloaded methods, such as exceptions, just do the logging operation, some methods are not processed.
- Some data needs to be associated, and Appsession provides properties (Idictionary<object, object> type) for items that can hold up to a maximum of ten data.
- Additional objects are required to implement the custom protocol.
1 //in the following code, when a new connection is connected, the server sends a welcome message to the client immediately. The code also overrides other Appsession methods to implement its own business logic. 2 Public classTelnetsession:appsession<telnetsession>3 {4 //overloaded onsessionstarted function, endorsed appserver.newsessionconnected + = newsessionconnected5 protected Override voidonsessionstarted ()6 {7 //the logical part after the session link succeeds. 8 This. Send ("Welcome to SuperSocket Telnet Server");9 }Ten One protected Override voidhandleunknownrequest (stringrequestinfo requestinfo) A { - //logical part of receiving unknown request - This. Send ("Unknow Request"); the } - - protected Override voidonsessionclosed (closereason reason) - { + //logical code after a session is closed - Base. onsessionclosed (reason); + } A } at - //you can add new attributes to the session class based on your business needs . - Public classPlayersession:appsession<playersession> - { - Public intGamehallid {Get;Internal Set; } - in Public intRoomid {Get;Internal Set; } -}
In the code above, both of the custom appsession are used by the command-line protocol . Because of the generic constraints, when customizing the Appsession, the generic tappsession must specify the defined class , many friends like the command can not be loaded, the server does not start up and so on, because of the definition of errors caused by this.
A friend will ask, why not directly inherit appsession? Sorry, the Quick Start series does not do brain, in the first chapter of the content has been said.
Step Two: Create your appserver type
If you create your own appsession and want to use it, you must create the corresponding appserver.
1 //now Telnetsession will be able to be used in telnetserver sessions, and there are many ways to overload2 Public classTelnetserver:appserver<telnetsession>3 {4 protected Override BOOLSetup (irootconfig rootconfig, iserverconfig config)5 {6 //Modify the home configuration file accordingly. 7 return Base. Setup (rootconfig, config);8 }9 Ten protected Override voidOnstartup () One { A //logical part of server startup - Base. Onstartup (); - } the - protected Override voidonstopped () - { - //stopping the logical part of the server + Base. Onstopped (); - } +}
Step three: Start your server
Also recorded in the previous section of what we said, how to start your SS?
//the first method, the code starts. Static voidMain (string[] args) { //attention, Telnetserver . varAppServer =NewTelnetserver (); Appserver.setup ( -); //Start listening .Appserver.start (); while(Console.readkey (). KeyChar! ='Q') {Console.WriteLine (); Continue; } //Stop the server. appserver.stop ();}//The second method, through the configuration start, it should be noted that must pay attention to the configuration, otherwise the startup will fail, there is no decision to initialize the success or not, you can see the source of examples. Static voidMain (string[] args) { varBootstrap =Bootstrapfactory.createbootstrap (); Bootstrap. Initialize (); Bootstrap. Start (); while(Console.readkey (). KeyChar! ='Q') {Console.WriteLine (); Continue; } bootstrap. Stop ();}
Through the configuration start, it should be noted that must be configured correctly, otherwise it will not be able to start your appserver, where servertype= "AppServer full name, the class is in the Assembly", such as: Servertype= " SuperSocket.QuickStart.TelnetServer_StartByConfig.TelnetServer, SuperSocket.QuickStart.TelnetServer_ Startbyconfig ".
Here, how to start the SS and if you simply implement your AppServer and appsession and start your appserver complete, in the next section, we'll explain if the command is associated.
Advantages
Implementing your own Appsession and appserver allows you to easily expand SuperSocket based on your business needs, and you can bind session connections and disconnect events, and server instance start-up and stop events. You can also read your custom configuration information in the Setup method of AppServer. All in all, these features make it easy for you to create a socket server that you need to be possible.
SuperSocket Quick Start (iii): Achieve your appserver and appsession