SuperSocket Quick Start (3): implement your AppServer and AppSession,

Source: Internet
Author: User

SuperSocket Quick Start (3): implement your AppServer and AppSession,
What is AppSession?

AppSession represents a logical connection to the client. Connection-based operations should be defined in this class. You can use an instance of this type to send data to the client, receive the data sent by the client, or close the connection. You can also save the data associated with the client.

What is AppServer?

AppServer represents the server instance that listens to the client connection and carries the TCP connection. Ideally, we can use the AppServer instance to obtain any client connection you want. Server-level operations and logic should be defined in this class.

Step 1: Create Your AppSession

Why should I create an AppSession? In my opinion, the following three points are sufficient for the Quick Start System.

1 // In the following code, when a new connection is connected, the server immediately sends a welcome message to the client. This Code also overwrites other AppSession methods to implement your own business logic. 2 public class TelnetSession: AppSession <TelnetSession> 3 {4 // reload the OnSessionStarted function, which agrees with appServer. newSessionConnected + = NewSessionConnected 5 protected override void OnSessionStarted () 6 {7 // The logic section after the session link is successful. 8 this. send ("Welcome to SuperSocket Telnet Server"); 9} 10 11 protected override void HandleUnknownRequest (StringRequestInfo requestInfo) 12 {13 // logical part of the unknown request received 14 this. send ("Unknow request"); 15} 16 17 protected override void OnSessionClosed (CloseReason reason) 18 {19 // logic Code 20 base after the session is closed. onSessionClosed (reason); 21} 22} 23 24 // you can add a new attribute 25 public class PlayerSession to the Session class Based on your business needs: appSession <PlayerSession> 26 {27 public int GameHallId {get; internal set;} 28 29 public int RoomId {get; internal set;} 30}

In the above Code, both custom appsessions use the command line protocol. Due to generic constraints, during custom AppSession, generic TAppSession must specify the defined class. Many friends, such as the command cannot be loaded or the server cannot be started, this is because of definition errors.

Some may ask, why can't I inherit the AppSession directly? Sorry, the Quick Start series does not make up your mind. It has already been mentioned in Chapter 1.

Step 2: Create Your AppServer type

If you create your own AppSession and want to use it, you must create the corresponding AppServer.

1 // now TelnetSession can be used in TelnetServer sessions. There are also many ways to overload 2 public class TelnetServer: AppServer <TelnetSession> 3 {4 protected override bool Setup (IRootConfig rootConfig, IServerConfig config) 5 {6 // modify the home configuration file. 7 return base. setup (rootConfig, config); 8} 9 10 protected override void OnStartup () 11 {12 // server startup logic section 13 base. onStartup (); 14} 15 16 protected override void OnStopped () 17 {18 // logical part of stopping the server 19 base. onStopped (); 20} 21}
Step 3: Start your server

Record what we talked about in the previous section. How do I start your SS?

// The first method is code startup. Static void Main (string [] args) {// note that TelnetServer var appServer = new TelnetServer (); appServer. setup (2012); // start listening to appServer. start (); while (Console. readKey (). keyChar! = 'Q') {Console. WriteLine (); continue;} // stop the server. AppServer. stop () ;}// method 2. Start by configuring. Note that the configuration is correct. Otherwise, the startup will fail. It is not determined whether the initialization is successful or not, you can see examples in the source code. Static void Main (string [] args) {var bootstrap = BootstrapFactory. createBootstrap (); bootstrap. initialize (); bootstrap. start (); while (Console. readKey (). keyChar! = 'Q') {Console. WriteLine (); continue;} bootstrap. Stop ();}

By configuring startup, you must make sure that the configuration is correct. Otherwise, your AppServer cannot be started normally. serverType = "Full name of AppServer, assembly where the class is located", for example: serverType = "SuperSocket. quickStart. telnetServer_StartByConfig.TelnetServer, SuperSocket. quickStart. telnetServer_StartByConfig ".

At this point, how to start SS and how to simply implement your AppServer and AppSession and start your AppServer? In the next section, we will explain how to associate Command.

Advantages

The implementation of your own AppSession and AppServer allows you to easily expand SuperSocket according to your business needs, you can bind session connection and disconnection events, server instance startup and stop events. You can also read your custom configuration information in the Setup method of AppServer. All in all, these functions allow you to easily create a socket server you need.

References: http://docs.supersocket.net/v1-6/zh-CN/Implement-your-AppServer-and-AppSession

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.