protected IObservable <IStreamMessage> ConnectInternal(string endpoint, IEnumerable <KeyValuePair <string, object> > parameters = null) { return(Observable.Create <IStreamMessage>(async(observer, token) => { if (parameters != null && parameters.Any()) { endpoint += $"?{string.Join("&", AppClient.AsUrlParameter(parameters))}"; } var uri = new Uri(endpoint); try { WebSocketClient = new ClientWebSocket(); await WebSocketClient.ConnectAsync(uri, CancellationToken.None).Stay(); var buffer = new ArraySegment <byte>(new byte[1024]); observer.OnNext(new ConnectMessage()); while ((WebSocketClient.State == WebSocketState.Open || WebSocketClient.State == WebSocketState.CloseReceived) && !token.IsCancellationRequested) { var result = await WebSocketClient.ReceiveAsync(buffer, token).Stay(); if (result.MessageType == WebSocketMessageType.Close) { break; } var bytes = AsSafeBytes(buffer, result); // message greater than 1KB if (!result.EndOfMessage) { var stream = new MemoryStream(); stream.Write(bytes, 0, bytes.Length); do { result = await WebSocketClient.ReceiveAsync(buffer, token).Stay(); bytes = AsSafeBytes(buffer, result); stream.Write(bytes, 0, bytes.Length); } while (!result.EndOfMessage); bytes = stream.ToArray(); } if (result.MessageType == WebSocketMessageType.Text) { observer.OnNext(ParseData(Encoding.UTF8.GetString(bytes))); } } observer.OnCompleted(); } catch (Exception e) { // だいたいこっち来そうではある... observer.OnError(e); } return async() => await Disconnect().Stay(); })); }
public string AuthorizeUrl(string redirectUri, AccessScope scopes) { var parameters = new List <KeyValuePair <string, object> > { new KeyValuePair <string, object>("scope", scopes.ToString().ToLower().Replace(",", "")), new KeyValuePair <string, object>("response_type", "code"), new KeyValuePair <string, object>("redirect_uri", redirectUri), new KeyValuePair <string, object>("client_id", Client.ClientId) }; return($"https://{Client.Domain}/oauth/authorize?{string.Join("&", AppClient.AsUrlParameter(parameters))}"); }