Ejemplo n.º 1
0
        private void ProcessMessage(string nsPayload)
        {
            var msg      = JObject.Parse(nsPayload);
            var id       = msg["id"].Value(0);
            var accepted = msg["accepted"].Value(false);
            var targetId = msg["targetId"].Value(String.Empty);
            var @event   = msg["event"].Value(string.Empty);
            var error    = msg["error"].Value(string.Empty);
            var reason   = msg["reason"].Value(string.Empty);
            var data     = msg["data"].Value(string.Empty);

            // If a response retrieve its associated request.
            if (id > 0)
            {
                if (!_sents.TryGetValue(id, out Sent sent))
                {
                    _logger.LogError($"ProcessMessage() | received response does not match any sent request [id:{id}]");

                    return;
                }

                if (accepted)
                {
                    _logger.LogDebug($"ProcessMessage() | request succeed [method:{sent.RequestMessage.Method}, id:{sent.RequestMessage.Id}]");

                    sent.Resolve?.Invoke(data);
                }
                else if (!error.IsNullOrWhiteSpace())
                {
                    // 在 Node.js 实现中,error 的值可能是 "Error" 或 "TypeError"。
                    _logger.LogWarning($"ProcessMessage() | request failed [method:{sent.RequestMessage.Method}, id:{sent.RequestMessage.Id}]: {reason}");

                    sent.Reject?.Invoke(new Exception(reason));
                }
                else
                {
                    _logger.LogError($"ProcessMessage() | received response is not accepted nor rejected [method:{sent.RequestMessage.Method}, id:{sent.RequestMessage.Id}]");
                }
            }
            // If a notification emit it to the corresponding entity.
            else if (!targetId.IsNullOrWhiteSpace() && [email protected]())
            {
                if (@event == "running")
                {
                    RunningEvent?.Invoke(targetId);
                }
                MessageEvent?.Invoke(targetId, @event, data);
            }
            // Otherwise unexpected message.
            else
            {
                _logger.LogError($"ProcessMessage() | received message is not a response nor a notification: {nsPayload}");
            }
        }
Ejemplo n.º 2
0
 private static void OnResuming(RunningModeEventArgs args)
 {
     RunningEvent?.Invoke(null, args);
 }