Esempio n. 1
0
        /// <summary>
        /// 发送json格式的Http Rpc远程调用
        /// </summary>
        /// <typeparam name="T">结果对象的类型</typeparam>
        /// <returns>返回远程调用结果</returns>
        public async Task <T> Call <T>()
        {
            // 远程请求
            byte[] data = null;
            using (var request = CreateRequestMessage())
                using (var content = new PushStreamContent((ws) => RpcClientKit.WriteFrame(ws, _data, _isCompressed)))
                {
                    request.Content = content;
                    HttpResponseMessage response;
                    try
                    {
                        response = await _client.SendAsync(request).ConfigureAwait(false);
                    }
                    catch (Exception ex)
                    {
                        throw new ServerException("服务器连接失败", $"调用【{_methodName}】时服务器连接失败!\r\n{ex.Message}");
                    }

                    if (response.StatusCode != System.Net.HttpStatusCode.OK)
                    {
#if !SERVER
                        // 无权限时
                        if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
                        {
                            // 已登录则提示无权限
                            if (Kit.IsLogon)
                            {
                                throw new KnownException($"⚡对【{_methodName}】无访问权限!");
                            }

                            // 跳转到登录页面
                            Kit.Login(true);
                            throw new KnownException("请先登录您的账号!");
                        }
#endif
                        throw new ServerException($"服务器返回状态码:{response.StatusCode}", $"调用【{_methodName}】时返回状态码:{response.StatusCode}");
                    }

                    var stream = await response.Content.ReadAsStreamAsync();

                    data = await RpcClientKit.ReadFrame(stream);

                    response.Dispose();
                }
            return(ParseResult <T>(data));
        }
Esempio n. 2
0
        /// <summary>
        /// 读取从服务器返回的下一帧数据
        /// </summary>
        /// <returns></returns>
        public async Task <bool> MoveNext()
        {
            try
            {
                // _responseStream.ReadAsync 使用 CancellationToken 也只有第一次取消时有效,所以未使用!
                // 此处只在服务端取消连接时抛出异常!
                var data = await RpcClientKit.ReadFrame(_responseStream);

                _val = RpcKit.ParseBytes <object>(data);
                return(true);
            }
            catch
            {
                Dispose();
            }
            return(false);
        }