Пример #1
0
 public async Task SendAsync(MessageRequest msg)
 {
     if (msg.Id == null)
     {
         msg.Id = Guid.NewGuid().ToString();
     }
     var encoded = JsonConvert.SerializeObject(msg);
     var bytes   = Encoding.UTF8.GetBytes(encoded);
     await Socket.SendAsync(new ArraySegment <byte>(bytes),
                            WebSocketMessageType.Text, true,
                            CancellationToken.None).ConfigureAwait(false);
 }
Пример #2
0
        public async Task SendAsync(MessageRequest msg, Func <MessageResponse, bool?> callback, TimeSpan?timeout = null)
        {
            var callbackStruct = new MessageCallback
            {
                TaskCompleter = new TaskCompletionSource <bool>(),
                Expires       = Clock.Now.Add(timeout ?? TimeSpan.FromMinutes(1)),
                Callback      = callback
            };

            Listeners.TryAdd(msg.Id, callbackStruct);
            await SendAsync(msg);

            await callbackStruct.TaskCompleter.Task;
        }
Пример #3
0
 public MessageResponse(MessageRequest message)
 {
     Id     = message.Id;
     Path   = message.Path;
     Method = message.Method;
 }
Пример #4
0
        public Task Route(Request routerRequest)
        {
            MessageRequest message = null;
            var            hm      = (HandlerMessage)routerRequest.Context;

            switch (hm.Subprotocol)
            {
            case "rest.json":
                message = DeserializeJson(hm.Serialized);
                break;

            case "rest.msgpack":

                break;

            default:
                return(SendMessage(hm.Socket, new MessageResponse
                {
                    MessageType = MessageType.Error,
                    Status = 400,
                    Errors = new MessageData
                    {
                        ["Message"] =
                            $"Invalid protocol sent: `{hm.Subprotocol}`. Needs to be `rest.json` or `rest.msgpack`"
                    }
                }));
            }

            if (message == null)
            {
                return(SendMessage(hm.Socket, new MessageResponse
                {
                    MessageType = MessageType.Error,
                    Errors = new MessageData {
                        ["Message"] = "Invalid structure of request"
                    }
                }));
            }

            hm.Handler = this;
            hm.Request = message;

            try
            {
                using (var scope = _scopeFactory.CreateScope())
                {
                    return(Router.Dispatch(message.Path, message.Method, hm, scope.ServiceProvider));
                }
            }
            catch (Exception e)
            {
                while (e.InnerException != null)
                {
                    e = e.InnerException;
                }
                if (e is RouteNotFound)
                {
                    return(SendMessage(hm.Socket, new MessageResponse(message)
                    {
                        MessageType = MessageType.Error,
                        Status = 404,
                        Errors = new MessageData {
                            ["Message"] = "Route not found"
                        }
                    }));
                }

                ExceptionDispatchInfo.Capture(e).Throw();
            }

            return(Task.CompletedTask);
        }