public async Task TestProcAsync(Func <JsonRpc, CancellationToken, Task> mainFunc) { var cancellationTokenSrc = new CancellationTokenSource(); var cancellationToken = cancellationTokenSrc.Token; using (var socket = new ClientWebSocket()) { socket.Options.RemoteCertificateValidationCallback = (a, b, c, d) => true; var uri = new Uri(LyraGlobal.SelectNode(NetworkId)); var wssUrl = $"wss://{uri.Host}:{uri.Port}/api/v1/socket"; await socket.ConnectAsync(new Uri(wssUrl), cancellationToken); using (var jsonRpc = new JsonRpc(new WebSocketMessageHandler(socket))) { try { jsonRpc.AddLocalRpcMethod("Sign", new Func <string, string, string, Task <string[]> >( async(type, msg, accountId) => { var sign = await SignMessageAsync(msg); return(new string[] { "p1393", sign }); } )); jsonRpc.AddLocalRpcMethod("Notify", new Action <JObject>( (newsObj) => { var news = newsObj.ToObject <News>(); if (news.catalog == "Receiving") { RecvNotify(news.content as JObject); } } )); jsonRpc.StartListening(); await mainFunc(jsonRpc, cancellationToken); cancellationTokenSrc.Cancel(); await jsonRpc.Completion.WithCancellation(cancellationToken); } catch (OperationCanceledException) { // Closing is initiated by Ctrl+C on the client. // Close the web socket gracefully -- before JsonRpc is disposed to avoid the socket going into an aborted state. await socket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Client closing", CancellationToken.None); //throw; } catch { throw; } } } }
public static LyraRestClient Create(string networkId, string platform, string appName, string appVersion, string apiUrl = null) { //if (apiUrl == null) // return new LyraRestClient(networkId); var url = apiUrl ?? LyraGlobal.SelectNode(networkId) + "Node/"; var restClient = new LyraRestClient(platform, appName, appVersion, url); return(restClient); }
public static async Task <LyraRestClient> CreateAsync(string networkId, string platform, string appName, string appVersion, string apiUrl = null) { var url = apiUrl == null?LyraGlobal.SelectNode(networkId) + "LyraNode/" : apiUrl; var restClient = new LyraRestClient(platform, appName, appVersion, url); if (!await restClient.CheckApiVersion().ConfigureAwait(false)) { throw new Exception("Unable to use API. Must upgrade your App."); } else { return(restClient); } }
public static async Task <LyraRpcClient> CreateAsync(string networkId, string appName, string appVersion) { var httpClientHandler = new HttpClientHandler(); // Return `true` to allow certificates that are untrusted/invalid httpClientHandler.ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator; var httpClient = new HttpClient(httpClientHandler); var url = LyraGlobal.SelectNode(networkId).rpcUrl; var channel = GrpcChannel.ForAddress(url, new GrpcChannelOptions { HttpClient = httpClient }); var rpcClient = new LyraRpcClient(appName, appVersion, channel); if (!await rpcClient.CheckApiVersion()) { throw new Exception("Unable to use API. Must upgrade your App."); } return(rpcClient); }