1. 程式人生 > >.net core 和 WPF 開發升訊威線上客服與營銷系統:使用 TCP協議 實現穩定的客服端

.net core 和 WPF 開發升訊威線上客服與營銷系統:使用 TCP協議 實現穩定的客服端

本系列文章詳細介紹使用 .net core 和 WPF 開發 升訊威線上客服與營銷系統 的過程。本產品已經成熟穩定並投入商用。 線上演示環境:[https://kf.shengxunwei.com](https://kf.shengxunwei.com) 注意:演示環境僅供演示交流與評估,不保證 7x24 小時可用。 [文章目錄列表請點選這裡](https://blog.shengxunwei.com/Home/Post/44a31a32-d4e1-4ddd-8526-8a2bcd2e22be) --- 對於線上客服與營銷系統,客服端指的是後臺提供服務的客服或營銷人員,他們使用客服程式在後臺觀察網站的被訪情況,開展營銷活動或提供客戶服務。在本篇文章中,我將詳細介紹如何在 .net core 環境下使用 TCP 通訊技術實現穩定高效與安全的客服端程式。 這裡存在幾個技術難點需要注意: * 需要使客服端程式具備 24 小時不間斷執行的能力,在處理網路通訊時,必須100%的穩定。 * 必須具備應對網路波動的能力,不能網路稍有波動就斷線。即使出現了短暫的網路中斷,客服程式也不能做掉線處理,而是要具備保持和自動重連的能力。 * 要考慮安全性問題,服務端的埠監聽,要能識別正常客服端連線,還是來自攻擊者的連線。 訪客端實現的效果: ![](https://blog-api.shengxunwei.com/StaticFiles/Upload/eff58aa5-8c86-4d8f-8f54-aca3caca06d1.jpg) 訪客端在手機上的效果: ![](https://blog-api.shengxunwei.com/StaticFiles/Upload/26425a7b-3e19-4af4-92c9-3b80c1ec5c36.JPG) 後臺客服的實現效果: ![](https://blog-api.shengxunwei.com/StaticFiles/Upload/48026b95-5939-49cb-8175-5c0a33666924.JPG) --- ### 在服務端上通過 TcpListener 監聽客服連線 TcpListener類提供了簡單的方法,這些方法在阻止同步模式下偵聽和接受傳入連線請求。 可以使用 TcpClient 或 Socket 來連線 TcpListener 。 TcpListener使用 IPEndPoint 、本地 IP 地址和埠號或僅埠號建立一個。 Any為本地 IP 地址指定,如果希望基礎服務提供商為你分配這些值,則為0。 如果你選擇執行此操作,則可以在 LocalEndpoint 套接字連線後使用屬性來標識分配的資訊。 使用 Start 方法開始偵聽傳入連線請求。 Start將排隊傳入的連線,直到呼叫 Stop 方法或已將其排入佇列 MaxConnections 。 使用 AcceptSocket 或 AcceptTcpClient 從傳入連線請求佇列請求連線。 這兩種方法將會阻止。 如果要避免阻塞,可以 Pending 先使用方法來確定佇列中是否有連線請求。 呼叫 Stop 方法以關閉 TcpListener 。 下面的程式碼示例建立一個 TcpListener 。 ~~~csharp using System; using System.IO; using System.Net; using System.Net.Sockets; using System.Text; class MyTcpListener { public static void Main() { TcpListener server=null; try { // Set the TcpListener on port 13000. Int32 port = 13000; IPAddress localAddr = IPAddress.Parse("127.0.0.1"); // TcpListener server = new TcpListener(port); server = new TcpListener(localAddr, port); // Start listening for client requests. server.Start(); // Buffer for reading data Byte[] bytes = new Byte[256]; String data = null; // Enter the listening loop. while(true) { Console.Write("Waiting for a connection... "); // Perform a blocking call to accept requests. // You could also use server.AcceptSocket() here. TcpClient client = server.AcceptTcpClient(); Console.WriteLine("Connected!"); data = null; // Get a stream object for reading and writing NetworkStream stream = client.GetStream(); int i; // Loop to receive all the data sent by the client. while((i = stream.Read(bytes, 0, bytes.Length))!=0) { // Translate data bytes to a ASCII string. data = System.Text.Encoding.ASCII.GetString(bytes, 0, i); Console.WriteLine("Received: {0}", data); // Process the data sent by the client. data = data.ToUpper(); byte[] msg = System.Text.Encoding.ASCII.GetBytes(data); // Send back a response. stream.Write(msg, 0, msg.Length); Console.WriteLine("Sent: {0}", data); } // Shutdown and end connection client.Close(); } } catch(SocketException e) { Console.WriteLine("SocketException: {0}", e); } finally { // Stop listening for new clients. server.Stop(); } Console.WriteLine("\nHit enter to continue..."); Console.Read(); } } ~~~ ### 在客服端使用 TcpClient 連線到伺服器 TcpClient 類提供了在同步阻止模式下通過網路連線、傳送和接收流資料的簡單方法。 為了 TcpClient 連線和交換資料, TcpListener Socket 使用 TCP 建立的或 ProtocolType 必須偵聽傳入連線請求。 可以通過以下兩種方式之一連線到此偵聽器: * 建立一個 TcpClient 並呼叫三個可用方法中的一個 Connect 。 * TcpClient使用遠端主機的主機名和埠號建立一個。 此建構函式將自動嘗試連線。 我們使用下面的程式碼建立一個客服端連線程式: ~~~csharp static void Connect(String server, String message) { try { // Create a TcpClient. // Note, for this client to work you need to have a TcpServer // connected to the same address as specified by the server, port // combination. Int32 port = 13000; TcpClient client = new TcpClient(server, port); // Translate the passed message into ASCII and store it as a Byte array. Byte[] data = System.Text.Encoding.ASCII.GetBytes(message); // Get a client stream for reading and writing. // Stream stream = client.GetStream(); NetworkStream stream = client.GetStream(); // Send the message to the connected TcpServer. stream.Write(data, 0, data.Length); Console.WriteLine("Sent: {0}", message); // Receive the TcpServer.response. // Buffer to store the response bytes. data = new Byte[256]; // String to store the response ASCII representation. String responseData = String.Empty; // Read the first batch of the TcpServer response bytes. Int32 bytes = stream.Read(data, 0, data.Length); responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes); Console.WriteLine("Received: {0}", responseData); // Close everything. stream.Close(); client.Close(); } catch (ArgumentNullException e) { Console.WriteLine("ArgumentNullException: {0}", e); } catch (SocketException e) { Console.WriteLine("SocketException: {0}", e); } Console.WriteLine("\n Press Enter to continue..."); Console.Read(); } ~~~ --- 本文對使用 TCP 協議搭建客服端通訊框架進行了簡要的介紹,在接下來的文章中,我將具體解構服務端程式的結構和設計、客服端程式的結構和設計,敬請關注。 --- 線上演示環境:[https://kf.shengxunwei.com](https://kf.shengxunwei.com) 注意:演示環境僅供演示交流與評估,不保證 7x24 小時可用。 聯絡QQ: 279060597 聯絡E-mail:[email protected]