示例#1
0
        public async Task Send(IWsMessage message, CancellationToken cancellationToken = default)
        {
            await EnsureSocketConnection();

            var json   = JSerializer.Serialize(message);
            var data   = Encoding.UTF8.GetBytes(json);
            var buffer = new ArraySegment <byte>(data);
            await _socket.SendAsync(buffer, WebSocketMessageType.Text, true, cancellationToken);
        }
        public async Task Send(IWsMessage message, CancellationToken cancellationToken = default)
        {
            await EnsureSocketConnection();

            await Task.Run(() => _client.Send(JSerializer.Serialize(message)), cancellationToken);

            if (_configuration.ReconnectEnabled && _configuration.ResubscribeOnReconnect)
            {
                _subscriptions.Push(message);
            }
        }
        internal virtual async Task <TO> Post <TI, TO>(string path, TI payload, CancellationToken cancellationToken = default)
        {
            var request = new HttpRequestMessage(HttpMethod.Post, GetRequestUrl(path));

            if (payload != null)
            {
                request.Content = new StringContent(JSerializer.Serialize(payload), Encoding.UTF8, "application/json");
            }

            var client   = EnsureHttpClientCreated();
            var response = await client.SendAsync(request, cancellationToken);

            return(await GetResponseItem <TO>(path, response));
        }
        internal virtual async Task <T> Post <T>(string path, object payload, IDictionary <string, string> query = null,
                                                 IDictionary <string, string> customHeaders = null, CancellationToken cancellationToken = default)
        {
            var request = new HttpRequestMessage(HttpMethod.Post, BuildUri(path, query));

            if (payload != null)
            {
                var body = JSerializer.Serialize(payload);
                request.Content = new StringContent(body, Encoding.UTF8, "application/json");
            }

            FillRequestHeaders(request, customHeaders);

            using (var response = await _client.SendAsync(request, cancellationToken))
                return(await GetResponseItem <T>(response));
        }
示例#5
0
        /// <summary>
        /// 写入可序列化的对象。
        /// </summary>
        /// <typeparam name="TData">可序列化对象的类型。</typeparam>
        /// <param name="stream">可序列化的流。</param>
        /// <param name="data">可序列化的对象。</param>
        protected override void Writing <TData>(Stream stream, TData data)
        {
            var bytes = this.Encoding.GetBytes(_serializer.Serialize(data));

            stream.Write(bytes, 0, bytes.Length);
        }
示例#6
0
        public static string ToJson <T>(this T instance)
        {
            Check.Argument.IsNotNull(instance, "instance");

            return(JSerializer.Serialize(instance));
        }