예제 #1
0
        public async Task Excute(WebSocketConnection connection, string requestString)
        {
            var message = new WsMessage();

            try
            {
                var request = requestString.DeserializeJson <WsRequest>();
                message.Type   = WsMessageType.Result;
                message.Id     = request.Id;
                message.Method = request.Method;

                var session = _provider.GetService <WebSocketSession>();
                session.Connection = connection;
                session.Request    = request;

                var executor = _provider.GetService <WebSocketExecutor>();
                var result   = await executor.Excute(request);

                message.Result = result;
            }
            catch (InvokerException invokerException)
            {
                message.Result = invokerException.Message;
            }
            catch (Exception e)
            {
                message.Type   = WsMessageType.Error;
                message.Result = e.ToString();
            }
            finally
            {
                connection.PushMessage(message);
            }
        }
        public async Task Excute(WebSocketConnection connection, string requestString)
        {
            var message = new WsMessage();

            try
            {
                var request = requestString.DeserializeJson <WsRequest>();
                message.MsgType = WsMessageType.Result;
                message.Id      = request.Id;
                message.Method  = request.Method;

                var session = _provider.GetService <WebSocketSession>();
                session.Connection = connection;
                session.Request    = request;

                var executor = _provider.GetService <WebSocketExecutor>();
                var result   = await executor.Excute(request);

                if (result is WsError error)
                {
                    message.MsgType = WsMessageType.Error;
                    message.Error   = error;
                }
                else
                {
                    message.Result = result;
                }
            }
            catch (ArgumentException ex)
            {
                message.MsgType = WsMessageType.Error;
                message.Error   = new WsError()
                {
                    Code    = (int)ErrorCode.InvalidPara,
                    Message = ex.Message,
                };
            }
            catch (WsException wsEx)
            {
                message.MsgType = WsMessageType.Error;
                message.Error   = new WsError()
                {
                    Code    = wsEx.Code,
                    Message = wsEx.Message,
                };
            }
            catch (Exception e)
            {
                message.MsgType = WsMessageType.Error;
                message.Error   = new WsError()
                {
                    Code    = -1,
                    Message = e.ToString(),
                };
            }
            finally
            {
                connection.PushMessage(message);
            }
        }