美文网首页
三、Unity3D与SuperSocket开源项目跑得快棋牌游戏

三、Unity3D与SuperSocket开源项目跑得快棋牌游戏

作者: e29290bad2f3 | 来源:发表于2017-10-13 23:11 被阅读478次

    一、说明

    阅读本文前,推荐阅读

    二、服务器工程

    • 项目下载http://url.cn/5fsUmOi,密码tDdXNW
    • 本项目使用Visual Studio 2015
    • 使用nuget引用log4net,SuperSocket,SuperSocket.Engine程序包
    • 使用SuperSocket头部格式固定并且包含内容长度的协议模版(FixedHeaderReceiveFilter),头部固定两个字节,高字节在前,低字节再后,具体可参考代码
    using System;
    using System.Text;
    using SuperSocket.SocketBase.Protocol;
    using SuperSocket.Facility.Protocol;
    
    namespace Ping
    {
        public class PingFixedHeaderReceiveFilter : FixedHeaderReceiveFilter<BinaryRequestInfo>
        {
            public PingFixedHeaderReceiveFilter() : base(2)
            {
            }
    
            protected override int GetBodyLengthFromHeader(byte[] header, int offset, int length)
            {
                return (int)header[offset] * 256 + (int)header[offset + 1];
            }
    
            protected override BinaryRequestInfo ResolveRequestInfo(ArraySegment<byte> header, byte[] bodyBuffer, int offset, int length)
            {
                string cmd = Encoding.UTF8.GetString(bodyBuffer, offset, length);
                BinaryRequestInfo request = new BinaryRequestInfo(cmd, null);
                return request;
            }
        }
    }
    
    • 实现自己的PingServer,打印客户请求Key,并回复"PING",代码如下
    using System;
    using SuperSocket.SocketBase;
    using SuperSocket.SocketBase.Config;
    using SuperSocket.SocketBase.Protocol;
    
    namespace Ping
    {
        public class PingServer : AppServer<PingSession, BinaryRequestInfo>
        {
            public PingServer() : base(new DefaultReceiveFilterFactory<PingFixedHeaderReceiveFilter, BinaryRequestInfo>())
            {
    
            }
    
            protected override bool Setup(IRootConfig rootConfig, IServerConfig config)
            {
                return base.Setup(rootConfig, config);
            }
    
            protected override void OnStarted()
            {
                NewRequestReceived += OnNewRequestReceived;
    
                base.OnStarted();
            }
    
            protected override void OnStopped()
            {
                base.OnStopped();
            }
    
            void OnNewRequestReceived(PingSession session,BinaryRequestInfo info)
            {
                try
                {
                    Logger.Debug("request cmd : " + info.Key);
    
                    session.SendMessage();
                }
                catch (Exception ex)
                {
                    Logger.Error("ex : " + ex.StackTrace);
                }
    
                Logger.Debug("SessionCount : " + SessionCount + " TotalHandledRequests : " + TotalHandledRequests);
            }
        }
    }
    
    • 实现自己的PingSession,提供发送"PING"接口,代码如下
    using System;
    using System.Text;
    using SuperSocket.SocketBase;
    using SuperSocket.SocketBase.Protocol;
    
    namespace Ping
    {
        public class PingSession : AppSession<PingSession, BinaryRequestInfo>
        {
            #region override
            protected override void OnSessionStarted()
            {
                base.OnSessionStarted();
    
                Logger.Debug("OnSessionStarted session id : " + SessionID + " SessionCount: " + AppServer.SessionCount);
            }
    
            protected override void HandleUnknownRequest(BinaryRequestInfo requestInfo)
            {
                base.HandleUnknownRequest(requestInfo);
            }
    
            protected override void HandleException(Exception e)
            {
                base.HandleException(e);
            }
    
            protected override void OnSessionClosed(CloseReason reason)
            {
                base.OnSessionClosed(reason);
    
                Logger.Debug("OnSessionClosed session id : " + SessionID + " SessionCount: " + AppServer.SessionCount);
            }
            #endregion
    
            public void SendMessage()
            {
                byte[] messageData = Encoding.UTF8.GetBytes("PING");
                byte[] lenData = new byte[] { (byte)(messageData.Length / 256), (byte)(messageData.Length % 256) };//长度
                byte[] sendData = new byte[messageData.Length + lenData.Length];
                Array.Copy(lenData, 0, sendData, 0, lenData.Length);
                Array.Copy(messageData, 0, sendData, lenData.Length, messageData.Length);
                Send(sendData, 0, sendData.Length);
            }
        }
    }
    
    • 在Main函数启动监听,监听端口9090
    using System;
    
    namespace Ping
    {
        class Program
        {
            static int PORT = 9090;
    
            static void Main(string[] args)
            {
                StartSuperSocket();
            }
    
            static void StartSuperSocket()
            {
                var appServer = new PingServer();
                if (!appServer.Setup(PORT))
                {
                    Console.WriteLine("Failed to setup!");
                    Console.ReadKey();
                    return;
                }
    
                if (!appServer.Start())
                {
                    Console.WriteLine("Failed to start!");
                    Console.ReadKey();
                    return;
                }
    
                Console.WriteLine("The server started at port : "+ PORT + " successfully,press key 'q' to stop it");
    
                while (Console.ReadKey().KeyChar != 'q')
                {
                    Console.WriteLine();
                    continue;
                }
    
                appServer.Stop();
    
                Console.WriteLine("the server was stopped!");
                Console.ReadKey();
            }
        }
    }
    
    • 成功启动
    image.png

    相关文章

      网友评论

          本文标题:三、Unity3D与SuperSocket开源项目跑得快棋牌游戏

          本文链接:https://www.haomeiwen.com/subject/xsmtuxtx.html