首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> 關於.NET

利用 WCF P2P 實作區域網路剪貼簿同步

     編輯:關於.NET

傳統 WCF 架構需中央服務轉發訊息,增加伺服器負擔。WCF NetPeerTcpBinding 提供 P2P (Peer-to-Peer) 通訊模式,每個節點同時是 Client 與 Service,適合區域網路即時資料同步。本文以「跨電腦剪貼簿文字同步」為例,示範完整實作流程。

架構對比

  • Client-Server: 所有流量經中央伺服器,單點瓶頸。
  • P2P Mesh: 節點自動發現、形成網狀拓撲,資料直接廣播至所有對等節點。
Client-Server 架構 P2P Mesh 架構

核心難點與解法

  1. 監聽剪貼簿變更: 覆寫 WinForms WndProc,攔截 WM_DRAWCLIPBOARD (0x308) 訊息,搭配 SetClipboardViewer / ChangeClipboardChain 維護剪貼簿檢視器鏈結。
  2. P2P 通訊契約: 定義雙工契約 IShare,使用 NetPeerTcpBinding、SecurityMode.None、端點位址 net.p2p://SimpleP2P。

關鍵程式碼實作

1. 服務契約與實作

[ServiceContract(CallbackContract = typeof(IShare))]
public interface IShare
{
    [OperationContract(IsOneWay = true)]
    void ShareClipboard(string type, string message);
}

public class ShareImplementation : IShare
{
    private static Form m_receiverForm;
    private static ClipEventHandler m_OnClipReceive;

    public void ShareClipboard(string type, string message)
    {
        try { m_receiverForm.Invoke(m_OnClipReceive, type, message); }
        catch (Exception e) { MessageBox.Show(e.ToString()); }
    }

    public void SetForm(Form form, ClipEventHandler callback)
    {
        m_receiverForm = form;
        m_OnClipReceive = callback;
    }
}

public delegate void ClipEventHandler(string type, string clipData);

2. Peer 管理類別 (封裝通道生命週期)

public class Peer
{
    public string Id { get; private set; }
    public IShare Channel;
    public ShareImplementation Host;
    public Form Form;
    public ClipEventHandler ClipEventHandler;

    private DuplexChannelFactory<IShare> _factory;
    private readonly AutoResetEvent _stopFlag = new AutoResetEvent(false);

    public Peer(string id) { Id = id; }

    public void StartService()
    {
        var binding = new NetPeerTcpBinding { Security = { Mode = SecurityMode.None } };
        var endpoint = new ServiceEndpoint(
            ContractDescription.GetContract(typeof(IShare)),
            binding,
            new EndpointAddress("net.p2p://SimpleP2P"));

        Host = new ShareImplementation();
        Host.SetForm(Form, ClipEventHandler);

        _factory = new DuplexChannelFactory<IShare>(new InstanceContext(Host), endpoint);
        var channel = _factory.CreateChannel();
        ((ICommunicationObject)channel).Open();
        Channel = channel;
    }

    public void StopService()
    {
        ((ICommunicationObject)Channel).Close();
        if (_factory != null) _factory.Close();
    }

    public void Run()
    {
        StartService();
        _stopFlag.WaitOne();
        StopService();
    }

    public void Stop() { _stopFlag.Set(); }
}

3. WinForm 事件處理與 UI 更新

protected override void WndProc(ref Message m)
{
    const int WM_DRAWCLIPBOARD = 0x308;
    const int WM_CHANGECBCHAIN = 0x030D;
    switch (m.Msg)
    {
        case WM_DRAWCLIPBOARD:
            DisplayClipboardData();
            SendMessage(nextClipboardViewer, m.Msg, m.WParam, m.LParam);
            break;
        case WM_CHANGECBCHAIN:
            if (m.WParam == nextClipboardViewer) nextClipboardViewer = m.LParam;
            else SendMessage(nextClipboardViewer, m.Msg, m.WParam, m.LParam);
            break;
        default: base.WndProc(ref m); break;
    }
}

void DisplayClipboardData()
{
    if (Clipboard.ContainsText() && peer?.Channel != null)
    {
        string msg = Clipboard.GetText();
        peer.Channel.ShareClipboard("text", msg);
    }
}

public void AddToClip(string type, string message)
{
    if (type == "text") richTextBox1.Text = message;
    // 可擴充 RTF、圖片、檔案等格式
}

執行效果

  • 電腦 A (zhuqilin) 複製文字。
  • 電腦A 複製文字
  • 電腦 B (Colin) 即時顯示同步內容。
  • 電腦B 顯示同步文字

擴充方向

  1. 直接寫入遠端剪貼簿,支援 Ctrl+V 貼上。
  2. 支援檔案、圖片、RTF 等多格式同步 (參考 RealVNC 剪貼簿共享機制)。

注意: P2P 管道建立需數秒,啟動後請稍候再測試。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved