예제 #1
0
 private void button1_Click(object sender, EventArgs e)
 {
     connection = new Microsoft.AspNet.SignalR.Client.HubConnection(ServerUri);
     //类名必须与服务端一致
     //myHub = connection.CreateHubProxy("BroadcastHub");
     rhub = connection.CreateHubProxy("myhub");
     connection.Start();//连接服务器
     label1.Text = "连接服务器成功!";
     //注册客户端方法名称"addMessage"与服务器端Send方法对应,对应的 callback方法 ReceiveMsg
     rhub.On <string, string>("addMessage", ReceiveMsg);
 }
예제 #2
0
        async void IniSignalR(string serverUri)
        {
            connection = new Microsoft.AspNet.SignalR.Client.HubConnection(serverUri);
            //类名必须与服务端一致
            //myHub = connection.CreateHubProxy("BroadcastHub");
            rhub = connection.CreateHubProxy("MyHub");


            ////注册客户端方法名称"addMessage"与服务器端Send方法对应,对应的 callback方法 ReceiveMsg
            rhub.On("Register", Register);
            await connection.Start();//连接服务器
        }
예제 #3
0
        public bool Register(Func<string, string, bool> FncBroad, Func<string, string, bool> FncPri
            , Func<string, string, bool> FncSysUpdate = null)
        {
            try
            {
                HubConn = new Microsoft.AspNet.SignalR.Client.HubConnection(HostUrl);

                HubProxy = HubConn.CreateHubProxy("DefHub");

                HubConn.Headers.Add(hdrdb, Ultra.Web.Core.Common.ByteStringUtil.ByteArrayToHexStr(Encoding.UTF8.GetBytes(Corp)));
                HubConn.Headers.Add(hdrky, Ultra.Web.Core.Common.ByteStringUtil.ByteArrayToHexStr(Encoding.UTF8.GetBytes(UserName)));
                HubProxy.On<string, string>("broadCastMsg", (s, m) => FncBroad(s, m));
                HubProxy.On<string, string>("addMsg", (s, m) => FncPri(s, m));
                if (null != FncSysUpdate)
                    HubProxy.On<string, string>("systemUpdate", (s, m) => FncSysUpdate(s, m));
                HubProxy.On<string>("heartBeat", s => HeartBeat(s));
                //HubProxy.On<string, string>("broadCastMsg", (s, m) =>
                //    this.Invoke((Action)(() =>
                //    {
                //        rtb.AppendText(string.Format("{0} 发送了消息:{1}" + Environment.NewLine, s, m));
                //    })));
                //HubProxy.On<string, string>("addMsg", (s, m) => this.Invoke((Action)(() =>
                //  rtb2.AppendText(string.Format("{0} 给你发送:{1}" + Environment.NewLine, s, m)))));
                HubConn.Start().Wait();
                if (null != Theartbeat && Theartbeat.IsAlive)
                {
                    try
                    {
                        Theartbeat.Abort();
                    }
                    catch { }
                }
                if (EnableHeartBeat)
                {
                    Theartbeat = new System.Threading.Thread(() =>
                    {
                        while (true)
                        {
                            System.Threading.Thread.Sleep(5000);
                            SendHeartBeat("KY-HBT");
                        }
                    });
                    Theartbeat.IsBackground = true;
                    Theartbeat.SetApartmentState(System.Threading.ApartmentState.STA);
                    Theartbeat.Start();
                }
                return true;
            }
            catch (Exception)
            {
                throw;
                return false;
            }
        }