1.官方文档实现你的AppServer和AppSession
2.上一篇文章文档补充实现你的AppServer和AppSession[附加一个小知识]
既然我们用AppServer和AppSession就可以直接用来通讯交互,那么为什么还要实现我们自己的AppServer和AppSession,官网文档说了有下面的优点。
实现你自己的AppSession和AppServer允许你根据你业务的需求来方便的扩展SuperSocket,你可以绑定session的连接和断开事件,服务器实例的启动和停止事件。你还可以在AppServer的Setup方法中读取你的自定义配置信息。总而言之,这些功能让你方便的创建一个你所需要的socket服务器成为可能。
这篇文章先用一个小例子来说明扩展自己的AppSession和AppServer来实现一些简单的业务,扩展自定义配置信息的内容将放到下一个文章。
就列举一个很简单的例子,有多种客户端类型的通讯例子,暂时这个例子是使用同一种协议的,完整的例子是,客户端连接上服务器后,客户的告诉服务器自己是哪种类型的客户的,然后服务器就标记类型,然后发其他数据的时候,就可以根据客户的类型来选择不同的处理逻辑。
服务器代码非常简单如下:
MyServer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SuperSocket.SocketBase;
namespace WhyMySelf
{
public class MyServer:AppServer<MySession>
{
}
}
MySession
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SuperSocket.SocketBase;
using SuperSocket.SocketBase.Protocol;
namespace WhyMySelf
{
public class MySession : AppSession<MySession,StringRequestInfo>
{
public string ClientType { get; set; }
}
}
ClientType
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SuperSocket.SocketBase.Command;
using SuperSocket.SocketBase.Protocol;
namespace WhyMySelf
{
public class ClientType : CommandBase<MySession, StringRequestInfo>
{
public override void ExecuteCommand(MySession session, StringRequestInfo requestInfo)
{
Console.WriteLine("客户端[{0}]发送了标记",requestInfo.Body);
session.ClientType = requestInfo.Body;
}
}
}
Message
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SuperSocket.SocketBase.Command;
using SuperSocket.SocketBase.Protocol;
namespace WhyMySelf
{
public class Message : CommandBase<MySession, StringRequestInfo>
{
public override void ExecuteCommand(MySession session, StringRequestInfo requestInfo)
{
Console.WriteLine("客户端的类型是:{0},收到的消息是{1}",
session.ClientType,requestInfo.Body);
}
}
}
启动程序:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WhyMySelf
{
class Program
{
static void Main(string[] args)
{
MyServer msgPackServer = new MyServer();
//Setup the appServer
if (!msgPackServer.Setup(2012))
{
Console.WriteLine("Failed to setup!");
Console.ReadKey();
return;
}
Console.WriteLine();
//Try to start the appServer
if (!msgPackServer.Start())
{
Console.WriteLine("Failed to start!");
Console.ReadKey();
return;
}
Console.WriteLine("The server started successfully, press key 'q' to stop it!");
while (Console.ReadKey().KeyChar != 'q')
{
Console.WriteLine();
continue;
}
//Stop the appServer
msgPackServer.Stop();
Console.WriteLine("The server was stopped!");
Console.ReadKey();
}
}
}
客户端是开启两个线程来模拟两个客户端的,代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace WhyMyselfClient
{
class Program
{
static void Main(string[] args)
{
var threada = new Thread(SendA);
threada.Start();
var threadb = new Thread(SendB);
threadb.Start();
Console.ReadKey();
}
private static void SendA()
{
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect("127.0.0.1", 2012);
if (socket.Connected)
{
socket.Send(Encoding.UTF8.GetBytes("ClientType A_Client\r\n"));
Thread.Sleep(1000);
for (int i = 0; i < 10; i++)
socket.Send(Encoding.UTF8.GetBytes("Message AAAAAAAA\r\n"));
}
}
private static void SendB()
{
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect("127.0.0.1", 2012);
if (socket.Connected)
{
socket.Send(Encoding.UTF8.GetBytes("ClientType B_Client\r\n"));
Thread.Sleep(1000);
for (int i = 0; i < 10; i++)
socket.Send(Encoding.UTF8.GetBytes("Message BBBBBBBBB\r\n"));
}
}
}
}
运行后,服务器打印出来的信息如下:
客户端[B_Client]发送了标记
客户端[A_Client]发送了标记
客户端的类型是:B_Client,收到的消息是BBBBBBBBB
客户端的类型是:A_Client,收到的消息是AAAAAAAA
客户端的类型是:A_Client,收到的消息是AAAAAAAA
客户端的类型是:A_Client,收到的消息是AAAAAAAA
客户端的类型是:A_Client,收到的消息是AAAAAAAA
客户端的类型是:A_Client,收到的消息是AAAAAAAA
客户端的类型是:A_Client,收到的消息是AAAAAAAA
客户端的类型是:A_Client,收到的消息是AAAAAAAA
客户端的类型是:A_Client,收到的消息是AAAAAAAA
客户端的类型是:A_Client,收到的消息是AAAAAAAA
客户端的类型是:A_Client,收到的消息是AAAAAAAA
客户端的类型是:B_Client,收到的消息是BBBBBBBBB
客户端的类型是:B_Client,收到的消息是BBBBBBBBB
客户端的类型是:B_Client,收到的消息是BBBBBBBBB
客户端的类型是:B_Client,收到的消息是BBBBBBBBB
客户端的类型是:B_Client,收到的消息是BBBBBBBBB
客户端的类型是:B_Client,收到的消息是BBBBBBBBB
客户端的类型是:B_Client,收到的消息是BBBBBBBBB
客户端的类型是:B_Client,收到的消息是BBBBBBBBB
客户端的类型是:B_Client,收到的消息是BBBBBBBBB
可以看到实现我们的业务逻辑的代码是非常简单的,如果说不去实现自己的AppServer和AppSession,那么要实现这个多客户的类型的逻辑就会相对比较麻烦了。大家可以根据这个思路去扩展更多自己的业务逻辑。