Example #1
0
        static void Main(string[] args)
        {
            var hub   = new HubConnection("http://localhost/PushServe/signalr");
            var proxy = hub.CreateHubProxy("pushServeHub");

            hub.Error += (ex) => { Console.WriteLine("conn error!" + ex.Message); };
            hub.Start().Wait();
            if (hub.State == ConnectionState.Connected)
            {
                proxy.On("push", (resMsg) =>
                {
                    Console.WriteLine("服务返回:" + resMsg);
                });

                NewPushMsgPack data = new NewPushMsgPack()
                {
                    uid     = "100001",
                    resdata = "response data\t" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
                };
                ClientMsgPackEntity msg = new ClientMsgPackEntity()
                {
                    mp_type   = MpTypeEnum.NewMsgPush,
                    mp_cotent = data
                };

                proxy.Invoke("send", msg).Wait();
            }

            Console.ReadKey();
        }
Example #2
0
 public void Send(ClientMsgPackEntity pack)
 {
     if (pack != null)
     {
         IMsgProcess msgOp = MsgProcessFactory.GetInstance(pack.mp_type);
         msgOp.MsgProcess(pack, Clients, this.Context.ConnectionId);
     }
 }
Example #3
0
        public void MsgProcess(ClientMsgPackEntity value, IHubCallerConnectionContext <dynamic> clients = null, string connectionId = "")
        {
            if (value != null)
            {
                GloableMsgPack gloableMsgPack = JsonUtil <GloableMsgPack> .Deserialize(value.mp_cotent.ToString());

                if (gloableMsgPack != null)
                {
                    clients.All.Push(gloableMsgPack.resdata);
                }
            }
        }
Example #4
0
        /// <summary>
        /// 客户端连接维护
        /// </summary>
        /// <param name="value"></param>
        /// <param name="clients"></param>
        /// <param name="connectionId"></param>
        public void MsgProcess(ClientMsgPackEntity value, IHubCallerConnectionContext <dynamic> clients = null, string connectionId = "")
        {
            if (value != null)
            {
                ClientConnMsgPack clientPack = JsonUtil <ClientConnMsgPack> .Deserialize(value.mp_cotent.ToString());

                if (clientPack != null)
                {
                    Parallel.Invoke(
                        () => { AddClientInfo(connectionId, clientPack); },
                        () => { AddClientConnInfo(connectionId, clientPack); }
                        );
                }
            }
        }
        public void MsgProcess(ClientMsgPackEntity value, IHubCallerConnectionContext <dynamic> clients = null, string connectionId = "")
        {
            Task <bool> ts = new Task <bool>((arg) =>
            {
                bool ret = ConvertUtil.ConvertObjectToBool(arg, false);
                try
                {
                    if (value != null)
                    {
                        NewPushMsgPack newMsgPack = JsonUtil <NewPushMsgPack> .Deserialize(value.mp_cotent.ToString());
                        if (newMsgPack != null)
                        {
                            List <string> connIds = RedisHelper.Hash_Get <List <string> >(Constant.RedisClusterConn, Constant.ClientConnKey, newMsgPack.uid);
                            if (connIds != null && connIds.Any())
                            {
                                if (clients != null)
                                {
                                    clients.Clients(connIds).Push(newMsgPack.resdata);
                                    ret = true;
                                }
                            }
                        }
                    }
                }
                catch { }
                return(ret);
            }, false);

            ts.Start();
            ts.ContinueWith(t =>
            {
                clients.Client(connectionId).Push(ts.Result ? "推送消息到客户端成功!" : "推送消息到客户端失败!");
            });

            clients.Client(connectionId).Push("服务端接受到消息!");
        }