public void OnMessageReceived(string msg) { var jsonObject = (JsonObject)JsonObject.Parse(msg); if (!jsonObject.TryGetValue("id", out var idValue)) { if (RpcNotificationArrived != null) { var args = new RpcNotificationEventArgs(jsonObject); RpcNotificationArrived(this, args); } return; } int id = int.Parse(idValue.ToString()); lock (this) { if (!mRequests.TryGetValue(id, out var request)) { throw new Exception("Request response " + id + " not found, data: " + msg); } if (jsonObject.TryGetValue("error", out JsonValue errorValue)) { request.mHandler.OnError(((JsonObject)errorValue)["message"].ToString(), id); } else { request.mHandler.OnResponse(jsonObject["result"], id); } mRequests.Remove(id); var expiredCalls = mRequests.Where(pair => pair.Value.mTime <= DateTime.Now).Select(pair => pair).ToArray(); foreach (var pair in expiredCalls) { pair.Value.mHandler.OnError("timeout", pair.Key); mRequests.Remove(pair.Key); } } }
private static void MRequestManager_RpcNotificationArrived(RequestManager sender, RpcNotificationEventArgs args) { var json = args.Notification; var parameters = json["params"]; switch ((string)json["method"]) { case "On-ItemCreated": HandleRpcItemCreated(parameters); break; case "On-ItemDestroyed": HandleRpcItemDestroyed(parameters); break; case "On-ItemPropertyValueChanged": HandleRpcItemPropertyValueChanged(parameters); break; } }