Example #1
0
        internal void OnMessage(ResponseJsonMessage msg)
        {
            var a = subs.TryGetValue(msg.MsgType, out methodType method);

            if (a)
            {
                var dg   = method.action;
                var data = msg.GetData(method.type);
                dg.DynamicInvoke(data);
            }
        }
Example #2
0
        public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
        {
            var methodInfo = serviceInfo.GetMethod(binder.Name);
            var id         = Guid.NewGuid().ToString();
            var method     = ServiceType.GetMethod(binder.Name);
            var returnType = method.ReturnType;
            var request    = new RequestJsonMessage
            {
                Service = ServiceName,
                Method  = binder.Name,
                //Token = clientConnect.Token
            };
            var allArgs = method.GetParameters();
            var token   = request.Token;

            request.Token = CreateAccessToken(allArgs, args.ToList(), clientConnect.TokenInfo);
            request.Args  = args.ToList();
            ResponseJsonMessage response = null;

            try
            {
                response = ((WebSocketClientConnect)clientConnect).SendRequest(request);
            }
            catch (Exception ero)
            {
                ThrowError(ero.Message, "500");
            }
            if (response == null)
            {
                ThrowError("请求超时未响应", "500");
            }
            if (!response.Success)
            {
                ThrowError($"服务端处理错误:{response.Msg}", response.Data);
            }
            if (response.Outs != null && response.Outs.Count > 0)
            {
                foreach (var kv in response.Outs)
                {
                    var p     = allArgs[kv.Key];
                    var value = kv.Value;
                    if (p.Name.EndsWith("&"))
                    {
                        var name  = p.Name.Replace("&", "");
                        var type2 = Type.GetType(name);
                        value = value.ToString().ToObject(type2);
                    }
                    args[kv.Key] = value;
                }
            }
            if (!string.IsNullOrEmpty(response.Token))
            {
                clientConnect.TokenInfo.Token = response.Token;
            }
            if (returnType == typeof(void))
            {
                result = null;
                return(true);
            }
            var generType = returnType;

            if (methodInfo.IsAsync)
            {
                generType = returnType.GenericTypeArguments[0];
            }
            result = response.GetData(generType);
            if (methodInfo.IsAsync)
            {
                var result2 = result;
                var task    = methodInfo.TaskCreater();
                task.ResultCreater = async() =>
                {
                    return(await Task.FromResult(result2));
                };
                result = task.InvokeAsync();
            }
            return(true);
        }
Example #3
0
        public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
        {
            var id              = Guid.NewGuid().ToString();
            var method          = ServiceType.GetMethod(binder.Name);
            var methodParamters = method.GetParameters();
            var returnType      = method.ReturnType;
            var request         = new RequestJsonMessage
            {
                Service = ServiceName,
                Method  = binder.Name,
                //Token = clientConnect.Token.Token
            };
            var allArgs = method.GetParameters();

            request.Args = args.ToList();
            ResponseJsonMessage response = null;

            try
            {
                response = SendRequest(methodParamters, request);
            }
            catch (Exception ero)
            {
                ThrowError(ero.Message, "500");
            }
            if (response == null)
            {
                ThrowError("请求超时未响应", "500");
            }
            if (!response.Success)
            {
                ThrowError($"服务端处理错误:{response.Msg}", response.Data);
            }
            if (response.Outs != null && response.Outs.Count > 0)
            {
                foreach (var kv in response.Outs)
                {
                    var p     = allArgs[kv.Key];
                    var value = kv.Value;
                    if (p.Name.EndsWith("&"))
                    {
                        var name  = p.Name.Replace("&", "");
                        var type2 = Type.GetType(name);
                        value = value.ToString().ToObject(type2);
                    }
                    args[kv.Key] = value;
                }
            }
            if (!string.IsNullOrEmpty(response.Token))
            {
                clientConnect.TokenInfo.Token = response.Token;
            }
            if (returnType == typeof(void))
            {
                result = null;
                return(true);
            }
            result = response.GetData(returnType);

            return(true);
        }