Пример #1
0
        public new Task <JsonRpcResponse> Send(JsonRpcRequest request)
        {
            var tcs = new TaskCompletionSource <JsonRpcResponse>();

            _requestList.TryAdd(request.ID, tcs);
            base.Send(request).ContinueWith(t => {
                if (!t.Result)
                {
                    OnReceiveMessage(JsonRpcMessages.CreateError(request.ID, "send failed"));
                }
            });

            return(tcs.Task);
        }
Пример #2
0
        protected override async Task OnReceiveMessage(JsonRpcRequest msg)
        {
            if (msg != null)
            {
                if (string.Equals(msg.Method, JsonRpcMessages.PingRequest.Method, StringComparison.InvariantCultureIgnoreCase))
                {
                    await base.Send(JsonRpcMessages.GetPongResponse(msg.ID));

                    return;
                }

                try
                {
                    //invoke mvc handle
                    HttpContext context = null;
                    if ((context = await CobWebSocket2HttpContextBridge.Invoke(_context, msg)) != null)
                    {
                        string body = "";
                        using (var ms = context.Response.Body)
                        {
                            using (var sr = new StreamReader(ms))
                            {
                                body = await sr.ReadToEndAsync();
                            }
                        }

                        if (context.Response.StatusCode == 200)
                        {
                            var res = new JsonRpcResponse()
                            {
                                ID = msg.ID, Result = JsonConvert.DeserializeObject(body)
                            };
                            foreach (var header in context.Response.Headers)
                            {
                                res.Properties[header.Key] = header.Value;
                            }

                            base.SendAndForget(res);//todo:编解码了多次

                            return;
                        }
                        else
                        {
                            var error = JsonRpcMessages.CreateError(msg.ID, context.Response.StatusCode, ((HttpStatusCode)context.Response.StatusCode).ToString());
                            error.Error.Data = body;
                            base.SendAndForget(error);

                            return;
                        }
                    }

                    base.SendAndForget(JsonRpcMessages.CreateError(msg.ID, "can not route to action"));

                    return;
                }
                catch (Exception ex)
                {
                    base.SendAndForget(JsonRpcMessages.CreateError(msg.ID, ex.Message));

                    throw ex;
                }
            }

            base.SendAndForget(JsonRpcMessages.CreateError("msg is empty"));
        }