.net SignalR winform 推送廣播


最近在做一個項目,需要用到服務端主動推送給客戶端,最開始用的是自己比較順手的Remoting,可后來發現把服務端架到外網上,就猴子它哥了,后來又嘗試WCF,雖然能推送,但是推了幾次也猴子它哥了,后來找到了SignalR,關於這個通訊框架的資料,自己去查查,很方便的,但是關於winform這方面的資料特別少,全是web的,以免大家重蹈覆轍,所以就寫下這篇文章。

服務端代碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNet.SignalR;

namespace SignalRServer
{
    public class BroadcastContext
    {
        //單例模式 所有客戶端都共用此實例 才能達到廣播的效果
        private static readonly BroadcastContext instance =
        new BroadcastContext(GlobalHost.ConnectionManager.GetHubContext<BroadcastHub>());

        private readonly IHubContext context;
        //功能業務類
        private readonly FunctionApp functionApp;

        public static BroadcastContext Instance
        {
            get { return instance; }
        }

        private BroadcastContext(IHubContext context)
        {
            this.context = context;
            functionApp = new FunctionApp(context);
            functionApp.Action();
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNet.SignalR;

namespace SignalRServer
{
    public class BroadcastHub : Hub
    {
        private readonly BroadcastContext broadcastContext;

        public BroadcastHub()
        {
            broadcastContext = BroadcastContext.Instance;
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Owin.Cors;
using Owin;

namespace SignalRServer
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.UseCors(CorsOptions.AllowAll);
            app.MapSignalR();
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNet.SignalR;
using System.Threading;

namespace SignalRServer
{
    public class FunctionApp
    {
        //上下文
        private readonly IHubContext context;

        public FunctionApp(IHubContext context)
        {
            this.context = context;
        }

        string[] actons = {"6:30 起床了","7:00 做廣播體操","7:30 吃早餐","8:00 該干嘛干嘛" };
        Random r = new Random();

        public void Action()
        {
            //測試廣播功能
            Task.Run(() => Start());
        }

        private void Start()
        {
            while (true)
            {
                Thread.Sleep(500);
                //客戶端調用的方法名必須一致
                context.Clients.All.ReceiveMsg("ClientId=>"+r.Next(1,20),"Message=>"+actons[r.Next(0, 4)]);
            }
        }
    }
}

using Microsoft.Owin.Hosting;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SignalRServer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string url = "http://192.168.1.77:8086";

        private void Form1_Load(object sender, EventArgs e)
        {
            WebApp.Start<Startup>(url);
            lblInfo.Text = "服務啟動成功";
        }
    }
}

客戶端代碼:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.AspNet.SignalR.Client;

namespace SignalRClient
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        HubConnection connection = null;
        IHubProxy myHub = null;
        string url = "http://192.168.1.77:8086";

        private void Form1_Load(object sender, EventArgs e)
        {
            connection = new HubConnection(url);
            //類名必須與服務端一致
            myHub = connection.CreateHubProxy("BroadcastHub");

            //方法名必須與服務端一致
            myHub.On<string, string>("ReceiveMsg", ReceiveMsg);

            connection.Start().ContinueWith(task =>
            {
                if (!task.IsFaulted)
                {
                    richTextBox1.Text = "與服務器連接成功!\r\n";
                }
                else
                {
                    richTextBox1.Text = "與服務器連接失敗,請確認服務器是否開啟!\r\n";
                }
            }).Wait();
        }

        private void ReceiveMsg(string clientId,string message)
        {
            richTextBox1.Text += clientId+"   "+ message+ "\r\n";
        }
    }
}

完整代碼下載鏈接


免責聲明!

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



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