Example #1
0
        public static object ReadMessageViaRpc(string contract, string method, object[] objects)
        {
            var rabbitMqHelper = SingletonUtility.GetSingleton <RabbitMqHelper>();
            var id             = IdentityHelper.NewSequentialGuid().ToString("N");
            var s = new RpcDto
            {
                Id      = id,
                Content = new RpcMessageClientDto
                {
                    Method = method,
                    Params = objects
                }
            };

            rabbitMqHelper.SendMessage($"server:{contract}", "", "", s.ObjectToBytes(), 2);
            object result  = null;
            bool   receive = false;

            rabbitMqHelper.ReadMessage(id, (ob, ea) =>
            {
                result  = ea.Body.BytesToObject();
                receive = true;
                rabbitMqHelper.DeleteQueue(id);
            }, 2);
            var start = DateTime.Now.ToTimeStamp();

            while (!receive)
            {
                if (DateTime.Now.ToTimeStamp() - start > 3000)
                {
                    throw new Exception("等待超时");
                }
            }
            return(result);
        }
Example #2
0
        public static void RegisterRpcServer(string name, Process process)
        {
            var rabbitMqHelper = SingletonUtility.GetSingleton <RabbitMqHelper>();

            rabbitMqHelper.ReadMessage($"server:{name}", (ob, ea) =>
            {
                var r = ea.Body.BytesToObject <RpcDto>();
                var s = process(r);
                rabbitMqHelper.SendMessage(r.Id, "", "", s.ObjectToBytes(), 2);
            }, 2);
        }