微軟 Surface Laptop 2/Pro 7+ 10 月韌體更新:提高效能和穩定性
阿新 • • 發佈:2021-10-20
轉載於: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();