示例#1
0
        public async Task <KMSResponse> SendAsync(string rpcType, object @params)
        {
            int requestId = ++this.requestId;
            var request   = new
            {
                jsonrpc = "2.0",
                id      = requestId,
                method  = rpcType,
                @params
            };
            var jsonSetting = new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            };
            var jsonStr = JsonConvert.SerializeObject(request, jsonSetting);

            _logger.LogInformation(jsonStr);
            requests[requestId] = jsonStr;
            var buffer = Encoding.UTF8.GetBytes(jsonStr);
            await clientWebSocket.SendAsync(new ArraySegment <byte>(buffer), WebSocketMessageType.Text, true, CancellationToken.None);

            while (!repsonses.ContainsKey(requestId))
            {
                Thread.Sleep(100);
            }
            KMSResponse resp = repsonses[requestId];

            requests.TryRemove(requestId, out string _);
            repsonses.TryRemove(requestId, out _);
            if (resp.Error != null)
            {
                _logger.LogError(resp.Error.Message);
            }
            return(resp);
        }
示例#2
0
        public async Task <T> CreateAsync <T>(T instance) where T : KMSObject
        {
            KMSResponse r = await SendAsync("create", new
            {
                type = typeof(T).Name,
                instance.constructorParams
            });

            instance.client      = this;
            instance.id          = r.GetStringValue();
            objects[instance.id] = instance;
            return(instance);
        }