• C# SuperWebSocket服务端学习(二)


    首先需要下载DLL类库  地址详见:http://download.csdn.NET/detail/u011269801/9590935

    1,打开VS2012,新建一个控制台应用程序,选择.NET4.0版本

    2,添加引用    

    SuperSocket的dll文件(

    SuperSocket.Common.dll,

    SuperSocket.SocketBase.dll,

    SuperSocket.SocketEngine.dll)到此项目的引用 (版本选4.0)

    SuperWebSocket.dll   到此项目的引用

    添加 系统的  


    System.Configuration;

    System.Configuration.Install;  到此项目的引用

    添加命名空间:


    using SuperSocket.SocketBase;

    using SuperWebSocket;

    接下来请看实现

    1、Player

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
     
    namespace WebSocketSeviceConsole
    {
     
        public class Player
        {
            public string sessionID { get; set; }
     
            public string Name { get; set; }
            public float X { get; set; }
     
            public float Y { get; set; }
     
            public float Z { get; set; }
            public Player(string id)
            {
                this.sessionID = id;
                Name = sessionID.Substring(0, 6);
                X = -0.66666F;
                Y = 1.59666F;
                Z = 0;
            }
     
        }
    }

    2、MakeDataToString

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
     
    namespace WebSocketSeviceConsole
    {
     
        public class MakeDataToString
        {
            public static string PlayerString(Player p)
            {
                return IDstr(p) + Namestr(p) + Xstr(p) + Ystr(p) + Zstr(p);
            }
     
            public static string IDstr(Player p)
            {
                return "<id>" + p.sessionID + "</id>";
            }
     
            public static string Namestr(Player p)
            {
                return "<name>" + p.Name + "</name>";
            }
     
            public static string Xstr(Player p)
            {
                return "<X>" + p.X + "</X>";
            }
     
            public static string Ystr(Player p)
            {
                return "<Y>" + p.Y + "</Y>";
            }
     
            public static string Zstr(Player p)
            {
                return "<Z>" + p.Z + "</Z>";
            }
        }
    }
     
    3、WebSocketSeviceConsole
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
     
    using SuperSocket.SocketBase;
    using SuperWebSocket;
     

    namespace WebSocketSeviceConsole
    {
        class Program
        {
            static int ClientNum = 0;
            static void Main(string[] args)
            {
                Dictionary<string, Player> PlayerList = new Dictionary<string, Player>();
                List<Player> Player__List = new List<Player>();
                Console.WriteLine("SuperWebSocket(0.8).Source服务器 按任意键start the WebSocketServer!");
                Console.ReadKey();
                Console.WriteLine();
                var appServer = new WebSocketServer();
                if (!appServer.Setup(2000))
                {
                    Console.WriteLine("Failed to setup!");
                    Console.ReadKey();
                    return;
                }
                appServer.NewSessionConnected += new SessionHandler<WebSocketSession>(appServer_NewClientConnected);
                appServer.NewMessageReceived += new SessionHandler<WebSocketSession, string>(appServer_NewMessageReceived);
                appServer.SessionClosed += new SessionHandler<WebSocketSession, CloseReason>(appServer_SessionClosed);
                Console.WriteLine();
                if (!appServer.Start())
                {
                    Console.WriteLine("Failed to start!");
                    Console.ReadKey();
                    return;
                }
                Console.WriteLine("服务器启动成功, 按 'q' 退出服务器!");
                while (Console.ReadKey().KeyChar != 'q')
                {
                    Console.WriteLine();
                    continue;
                }
                appServer.Stop();
                Console.WriteLine();
                Console.WriteLine("The server was stopped!");
                Console.ReadKey();
            }
     

            static void appServer_NewClientConnected(WebSocketSession session)
            {
                session.Send("第一次给客户端发信息,服务器端: ");
                Player ps = new Player(session.SessionID);
                session.Send(MakeDataToString.PlayerString(ps));
                Console.WriteLine("客户端 :端口" + session.RemoteEndPoint.Port + "连接到服务器了!");
                ClientNum += 1;
                foreach (var ses in session.AppServer.GetAllSessions())
                {
                    ses.Send("xxx加入了连接!");
                }
            }
     
            static void appServer_NewMessageReceived(WebSocketSession session, string message)
            {
                session.Send("欢迎登陆本系统: ");
                Console.WriteLine("有客户端消息" + message);
                Console.WriteLine("客户端数目" + ClientNum.ToString());
                foreach (var ses in session.AppServer.GetAllSessions())
                {
                    ses.Send("给所有客户端广播发送的消息广播电台");
                }
            }
     
            static void appServer_SessionClosed(WebSocketSession session, CloseReason closeRs)
            {
                session.Close();
                Console.WriteLine("客户端" + session.RemoteEndPoint.Port + "断开了连接!");
                ClientNum -= 1;
                Console.WriteLine("客户端数目" + ClientNum.ToString());
            }
     
        }
    }
     
     
  • 相关阅读:
    解决页面报错: GEThttp://localhost:8080/favicon.ico 404 (Not Found)
    vs2019快捷键整理
    js保存图片至本地
    PHP输出方式的区别
    js运算精度问题
    Hadoop相关问题
    2015年10月5日 12:49:07
    Hive数据倾斜
    Hadoop错误日志
    使用maven开发MR
  • 原文地址:https://www.cnblogs.com/zxtceq/p/7280677.html
Copyright © 2020-2023  润新知