supersocket c# 教程

本教程是supersocket c# 教程,创建了一个server端,代码很少。supersocket client下期添加。第三期是关于supersocket protocalbuf的

一、添加dll引用

supersocket添加dll引用

添加下面这几个引用:

supersocket c#引用
supersocket c#

二、添加代码

using SuperSocket.SocketBase;
using SuperSocket.WebSocket;
using System;
using System.Linq;
using Teacher_ddy.Manager;
using Teacher_ddy.SocketHelper;

namespace lswebsocket
{
    public class SuperSocketServerX
    {
        private static WebSocketServer appServer = new WebSocketServer();
        
        public void InitServer(int port)
        {
            //Setup the appServer
            if (!appServer.Setup(port)) //Setup with listening port
            {
                Form1.AddLog("学员机Websocket server配置失败!*****");
                return;
            }

            appServer.NewSessionConnected += appServer_NewSessionConnected;
            appServer.SessionClosed += appServer_NewSessionClosed;
            appServer.NewDataReceived += new SessionHandler<WebSocketSession, byte[]>(AppServer_NewDataReceived);
            appServer.NewMessageReceived += appServer_NewMessageReceived;
        }

        public void StartServer()
        {
            //Try to start the appServer
            if (!appServer.Start())
            {
                Form1.AddLog("学员机Websocket server启动失败!*****");
                return;
            }
            Form1.AddLog("学员机Websocket server启动成功!");
        }

        public void StopServer()
        {
            //Stop the appServer
            appServer.Stop();
            Form1.AddLog("学员机Websocket server已正常关闭!");
        }

        public int GetSessionCount()
        {
            return appServer.SessionCount;
        }

        static void appServer_NewSessionConnected(WebSocketSession session)
        {
            Form1.AddLog("学员机有人进入");
        }

        static void appServer_NewSessionClosed(WebSocketSession session, CloseReason aaa)
        {
            Form1.AddLog("学员机有人离开" + session.SessionID +" 原因:"+ aaa.ToString());

            int rid = SuperSocketServerSubscribe.GetRoomIdBySidX(session.SessionID);
            if (rid != -1)
            {
                SuperSocketServerSubscribe.listRoom[rid].State = false;
            }
            else
            {
            }
        }

        private void AppServer_NewDataReceived(WebSocketSession session, byte[] value)
        {
            Form1.AddLog("学员机接收数组。");

            short indexRoom = (short)SuperSocketServerSubscribe.GetRoomIdBySidX(session.SessionID);

            int len = 2 + 2 + 2 + value.Length;

            byte[] bytes = new byte[len];

            Function.FromShort(indexRoom, out bytes[9], out bytes[10]);

            Function.FromShort(indexRoom, out bytes[11], out bytes[12]);

            short valuelen = (short)value.Length;

            Function.FromShort(valuelen, out bytes[13], out bytes[14]);

            Array.Copy(value, 0, bytes, 15, valuelen);

            // 转发
            ManagerSend ms = new ManagerSend(43, 02);
            ms.CopyArray(bytes);
            Qptcp.SendMsgHex("lsonline1", ms.sendBuff);
        }

        static void appServer_NewMessageReceived(WebSocketSession session, string message)
        {
            // 收到客户端信息
            Form1.AddLog("学员机接收str:"+message);

            string[] mesStrings = message.Split(' ');//userid|stopexam

            switch (mesStrings[0])
            {
                case "connect":
                {
                    int rid = SuperSocketServerSubscribe.GetRoomIdByUserID(mesStrings[1]);
                    if (rid != -1)
                    {
                        SuperSocketServerSubscribe.listRoom[rid].SidX = session.SessionID;
                    }
                    else
                    {
                    }
                }
                    break;
                case "stopexam":
                {
                    int rid = SuperSocketServerSubscribe.GetRoomIdByUserID(mesStrings[1]);
                    if (rid != -1)
                    {
                        SuperSocketServerSubscribe.listRoom[rid].State = false;
                    }
                    else
                    {
                    }
                }
                    break;
            }
            

//            byte[] s = new byte[]{ 0x01, 0x02, 0x03 };
//            session.Send(s,1,2);

//            session.Send("hahaha哈哈哈");
        }

        public void SendByte(WebSocketSession session, byte[] bytes)
        {
            session.Send(bytes,0,bytes.Length);
        }

        public void SendByteBySessionID(string sessionid, byte[] bytes)
        {
            appServer.GetSessionByID(sessionid).Send(bytes,0,bytes.Length);
        }
    }
}

发送字符串:

session.Send("hahaha哈哈哈");

发送数组

session.Send(bytes,0,bytes.Length);

三、c# supersocket server运行结果

c# supersocket server运行结果

四、附录

HTML版本的Websocket 客户端

本站原创,未经允许不得转载:lingkb » supersocket c# 教程
赞(0)

评论抢沙发