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


本系列文章詳細介紹使用 .net core 和 WPF 開發 升訊威在線客服與營銷系統 的過程。本產品已經成熟穩定並投入商用。
請訪問:https://kf.shengxunwei.com


文章目錄列表請點擊這里


對於在線客服與營銷系統,客服端指的是后台提供服務的客服或營銷人員,他們使用客服程序在后台觀察網站的被訪情況,開展營銷活動或提供客戶服務。在本篇文章中,我將詳細介紹如何在 .net core 環境下使用 TCP 通信技術實現穩定高效與安全的客服端程序。

這里存在幾個技術難點需要注意:

  • 需要使客服端程序具備 24 小時不間斷運行的能力,在處理網絡通信時,必須100%的穩定。
  • 必須具備應對網絡波動的能力,不能網絡稍有波動就斷線。即使出現了短暫的網絡中斷,客服程序也不能做掉線處理,而是要具備保持和自動重連的能力。
  • 要考慮安全性問題,服務端的端口監聽,要能識別正常客服端連接,還是來自攻擊者的連接。

訪客端實現的效果:

訪客端在手機上的效果:

后台客服的實現效果:


在服務端上通過 TcpListener 監聽客服連接

TcpListener類提供了簡單的方法,這些方法在阻止同步模式下偵聽和接受傳入連接請求。 可以使用 TcpClient 或 Socket 來連接 TcpListener 。 TcpListener使用 IPEndPoint 、本地 IP 地址和端口號或僅端口號創建一個。 Any為本地 IP 地址指定,如果希望基礎服務提供商為你分配這些值,則為0。 如果你選擇執行此操作,則可以在 LocalEndpoint 套接字連接后使用屬性來標識分配的信息。

使用 Start 方法開始偵聽傳入連接請求。 Start將排隊傳入的連接,直到調用 Stop 方法或已將其排入隊列 MaxConnections 。 使用 AcceptSocket 或 AcceptTcpClient 從傳入連接請求隊列請求連接。 這兩種方法將會阻止。 如果要避免阻塞,可以 Pending 先使用方法來確定隊列中是否有連接請求。
調用 Stop 方法以關閉 TcpListener 。

下面的代碼示例創建一個 TcpListener 。

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使用遠程主機的主機名和端口號創建一個。 此構造函數將自動嘗試連接。

我們使用下面的代碼建立一個客服端連接程序:

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


聯系QQ: 279060597
聯系E-mail:C5118@outlook.com


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM