C# fleck websocket使用

转载于:https://www.itspeeding.com/article/28

1、web页面

 1 <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
 2 <head>
 3     <meta charset="utf-8" />
 4     <title>WebSocket测试</title>
 5     <style>
 6         .div1
 7         {
 8             height:200px;   
 9             width:200px;
10             border:1px solid blue;
11             margin:auto;
12         }
13         h4
14         {
15             margin:auto;
16         }
17     </style>
18     <script>
19         var webSocket = {};
20         //创建websockt
21         function CreateWebSocket() {
22             webSocket = new WebSocket("ws://127.0.0.1:30000");
23             webSocket.onopen = WebSokectOnOpen;
24             webSocket.onmessage = WebSocketOnMessage;
25             webSocket.onclose = WebSocketOnClose;
26         };
27  
28         //建立连接事件
29         function WebSokectOnOpen() {
30             alert("已经打开连接!");
31             webSocket.Send("WebSocketCreate Success!");
32         };
33  
34         //监听事件
35         function WebSocketOnMessage(event) {
36             //监听来自客户端的数据
37             alert(event.data);
38         };
39  
40         function WebSocketOnClose() {
41             //监听来自客户端的数据
42             alert('和服务器断开连接');
43         };
44  
45         //发送事件
46         function WebSocketSendMsg() {
47             //获取text中的值
48             var text = document.getElementById("Text1").value;
49             //发送到服务器
50             webSocket.send(text);
51         };
52     </script>
53 </head>
54 <body οnlοad="CreateWebSocket()">
55     <div class="div1">
56         <h4>CSDN博客</h4>
57         <h4>By:LoveMiw</h4>
58         <input type="text" id="Text1" />
59         <input type="button" οnclick="WebSocketSendMsg()" value="发送数据" />
60     </div>
61 </body>
62 </html>

2、服务端C#代码 Nuget添加Fleck的引用,可以创建一个web应用程序

 1  //客户端url以及其对应的Socket对象字典
 2             IDictionary<string, IWebSocketConnection> dic_Sockets = new Dictionary<string, IWebSocketConnection>();
 3             //创建
 4 
 5             WebSocketServer server = new WebSocketServer("ws://0.0.0.0:30000");//监听所有的的地址
 6             //出错后进行重启
 7             server.RestartAfterListenError = true;
 8 
 9             //开始监听
10             server.Start(socket =>
11             {
12                 socket.OnOpen = () =>   //连接建立事件
13                 {
14                     //获取客户端网页的url
15                     string clientUrl = socket.ConnectionInfo.ClientIpAddress + ":" + socket.ConnectionInfo.ClientPort;
16                     dic_Sockets.Add(clientUrl, socket);
17                     Console.WriteLine(DateTime.Now.ToString() + "|服务器:和客户端网页:" + clientUrl + " 建立WebSock连接!");
18                 };
19                 socket.OnClose = () =>  //连接关闭事件
20                 {
21                     string clientUrl = socket.ConnectionInfo.ClientIpAddress + ":" + socket.ConnectionInfo.ClientPort;
22                     //如果存在这个客户端,那么对这个socket进行移除
23                     if (dic_Sockets.ContainsKey(clientUrl))
24                     {
25                         //注:Fleck中有释放
26                         //关闭对象连接 
27                         //if (dic_Sockets[clientUrl] != null)
28                         //{
29                         //dic_Sockets[clientUrl].Close();
30                         //}
31                         dic_Sockets.Remove(clientUrl);
32                     }
33                     Console.WriteLine(DateTime.Now.ToString() + "|服务器:和客户端网页:" + clientUrl + " 断开WebSock连接!");
34                 };
35                 socket.OnMessage = message =>  //接受客户端网页消息事件
36                 {
37                     string clientUrl = socket.ConnectionInfo.ClientIpAddress + ":" + socket.ConnectionInfo.ClientPort;
38                     Console.WriteLine(DateTime.Now.ToString() + "|服务器:【收到】来客户端网页:" + clientUrl + "的信息:\n" + message);
39                 };
40             });
41 
42             Console.ReadKey();
43             foreach (var item in dic_Sockets.Values)
44             {
45                 if (item.IsAvailable == true)
46                 {
47                     item.Send("服务器消息:" + DateTime.Now.ToString());
48                 }
49             }
50             Console.ReadKey();
51 
52             //关闭与客户端的所有的连接
53             foreach (var item in dic_Sockets.Values)
54             {
55                 if (item != null)
56                 {
57                     item.Close();
58                 }
59             }
60 
61             Console.ReadKey();

 

posted @ 2020-07-06 01:00  摩诘  阅读(2847)  评论(0编辑  收藏  举报