SuperSocket框架学习笔记2-构建SuperWebSocket服务器程序

SuperSocket框架学习笔记2-构建SuperWebSocket服务器程序

上一节简单介绍了 基本的SuperSocket服务器

这一节我们使用 SuperWebSocket构建一个 能与Unity3D通信的(Console控制台)服务器程序

嘎嘎  

先下载   需要的  DLL类库

服务端:

SuperSocket1.6.1 这个必备

http://www.supersocket.net/

SuperWebSocket 服务端必备

http://superwebsocket.codeplex.com/

 

客户端:

WebSocket4Net  客户端必备

http://websocket4net.codeplex.com/

 

一,让我们来编写服务器程序

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;

 

在Program中 写代码

直接上代码了 ,本人菜鸟, 写的渣渣,见笑了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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服务器\n 按任意键start the WebSocketServer!");
 
            Console.ReadKey();
            Console.WriteLine();
 
            var appServer = new WebSocketServer();
 
 
 
            //Setup the appServer
            if (!appServer.Setup(2000)) //Setup with listening port
            {
                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();
 
            //Try to start the appServer
            if (!appServer.Start())
            {
                Console.WriteLine("Failed to start!");
                Console.ReadKey();
                return;
            }
 
            Console.WriteLine("服务器启动成功, 按 ‘q‘ 退出服务器!");
 
            while (Console.ReadKey().KeyChar != ‘q‘)
            {
                Console.WriteLine();
                continue;
            }
 
            //Stop the appServer
            appServer.Stop();
 
            Console.WriteLine();
            Console.WriteLine("The server was stopped!");
            Console.ReadKey();
 
 
        }
 
 
 
        static void appServer_NewClientConnected(WebSocketSession session)
        {
 
            session.Send("第一次给客户端发信息Unity3DServer: ");
 
            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("欢迎登陆本系统LinMengUnity3DServer: ");
 
          
            Console.WriteLine("有客户端消息" + message);
 
           
            Console.WriteLine("客户端数目" + ClientNum.ToString());
            foreach (var ses in session.AppServer.GetAllSessions())
            {
                ses.Send("给所有客户端广播发送的消息LinMeng广播电台");
           
            }
        }
 
        static void appServer_SessionClosed(WebSocketSession session, CloseReason closeRs)
        {
            session.Close();
            Console.WriteLine("客户端" + session.RemoteEndPoint.Port + "断开了连接!");
            ClientNum -= 1;
 
        
            Console.WriteLine("客户端数目" + ClientNum.ToString());
 
        }

  

 

新建一个 Player类 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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;
       }
 
 
   }

  

 

新建一个 MakeDataToString类

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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>";
       }
 
 
   }

  

如果没有错误,启动成功,你将看到

SuperWebSocket(0.8).Source服务器\n 按任意键start the WebSocketServer!

随便按一个键

出现

服务器启动成功, 按 ‘q‘ 退出服务器!

 

说明服务器启动成功,否则请检查代码,或命名空间引用

QQ:2360450496

SuperSocket官方QQ群:373076764

欢迎大家来一起研究开发这个Socket框架!

 

 

 

 

 以上就是全部的 服务器程序代码,下一节我们将 编写Unity3D客户端程序,GOGOGO!

Coding!!!

 

 

 

SuperSocket框架学习笔记2-构建SuperWebSocket服务器程序,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。