protected override void OnStart(IConnection con, string conData, CancellationToken disconToken) { connection = con; connectionData = conData; var connectUrl = UrlBuilder.BuildConnect(connection, Name, connectionData); if (webSocket != null) { DisposeWebSocket(); } // SignalR uses https, websocket4net uses wss connectUrl = connectUrl.Replace("http://", "ws://").Replace("https://", "wss://"); IDictionary <string, string> cookies = new Dictionary <string, string>(); if (connection.CookieContainer != null) { var container = connection.CookieContainer.GetCookies(new Uri(connection.Url)); foreach (Cookie cookie in container) { cookies.Add(cookie.Name, cookie.Value); } } webSocket = new WebSocketWrapper(connectUrl, WebSocketOnMessageReceived); }
protected override void OnStart(IConnection con, string conData, CancellationToken disconToken) { connection = con; connectionData = conData; var connectUrl = UrlBuilder.BuildConnect(connection, Name, connectionData); // SignalR uses https, websocket4net uses wss connectUrl = connectUrl.Replace("http://", "ws://").Replace("https://", "wss://"); IDictionary <string, string> cookies = new Dictionary <string, string>(); if (connection.CookieContainer != null) { var container = connection.CookieContainer.GetCookies(new Uri(connection.Url)); foreach (Cookie cookie in container) { cookies.Add(cookie.Name, cookie.Value); } } WebSocket.Uri = new Uri(connectUrl); WebSocket.OnMessage = WebSocketOnMessageReceived; WebSocket.KeepAlive = TimeSpan.FromSeconds(5.0); WebSocket.Start(); }
private async Task Start(IConnection connection, string connectionData) { try { await StartWebSocket(connection, UrlBuilder.BuildConnect(connection, Name, connectionData)); } catch (TaskCanceledException) { TransportFailed(null); } catch (Exception ex) { TransportFailed(ex); } }
protected override void OnStart(IConnection con, string conData, CancellationToken disconToken) { connection = con; connectionData = conData; disconnectToken = disconToken; var connectUrl = UrlBuilder.BuildConnect(connection, Name, connectionData); try { PerformConnect(connectUrl); } catch (Exception ex) { TransportFailed(ex); } }
protected override void OnStart(IConnection con, string conData, CancellationToken disconToken) { connection = con; connectionData = conData; var connectUrl = UrlBuilder.BuildConnect(connection, Name, connectionData); if (websocket != null) { DisposeWebSocket(); } // SignalR uses https, websocket4net uses wss connectUrl = connectUrl.Replace("http://", "ws://").Replace("https://", "wss://"); IDictionary <string, string> cookies = new Dictionary <string, string>(); if (connection.CookieContainer != null) { var container = connection.CookieContainer.GetCookies(new Uri(connection.Url)); foreach (Cookie cookie in container) { cookies.Add(cookie.Name, cookie.Value); } } websocket = new BaseSocket(log, connectUrl, cookies, connection.Headers); websocket.OnError += WebSocketOnError; websocket.OnClose += WebSocketOnClosed; websocket.OnMessage += WebSocketOnMessageReceived; websocket.DataInterpreterString = interpreter; if (connection.Proxy != null) { var proxy = connection.Proxy.GetProxy(new Uri(connectUrl)); websocket.SetProxy(proxy.Host, proxy.Port); } if (!websocket.Connect().Result) { TransportFailed(new Exception("Can't connect")); } }
private string ResolveUrl(IConnection connection, string connectionData) { string url; if (connection.MessageId == null) { url = UrlBuilder.BuildConnect(connection, Name, connectionData); connection.Trace(TraceLevels.Events, "LP Connect: {0}", url); } else if (IsReconnecting(connection)) { url = UrlBuilder.BuildReconnect(connection, Name, connectionData); connection.Trace(TraceLevels.Events, "LP Reconnect: {0}", url); } else { url = UrlBuilder.BuildPoll(connection, Name, connectionData); connection.Trace(TraceLevels.Events, "LP Poll: {0}", url); } return(url); }
public virtual async Task PerformConnect() { await PerformConnect(UrlBuilder.BuildConnect(_connection, Name, _connectionData)); }
// For testing public virtual Task PerformConnect() { return(PerformConnect(UrlBuilder.BuildConnect(_connection, Name, _connectionData))); }
internal void OpenConnection(IConnection connection, string data, CancellationToken disconnectToken, bool reconnecting) { // If we're reconnecting add /connect to the url var url = reconnecting ? UrlBuilder.BuildReconnect(connection, Name, data) : UrlBuilder.BuildConnect(connection, Name, data); connection.Trace(TraceLevels.Events, "SSE: GET {0}", url); var getTask = HttpClient.Get(url, req => { _request = req; _request.Accept = "text/event-stream"; connection.PrepareRequest(_request); }, isLongRunning: true); var requestCancellationRegistration = disconnectToken.SafeRegister(state => { _stop = true; // This will no-op if the request is already finished. ((IRequest)state).Abort(); }, _request); getTask.ContinueWith(task => { if (task.IsFaulted || task.IsCanceled) { var exception = task.IsCanceled ? new OperationCanceledException(Resources.Error_TaskCancelledException) : task.Exception.Unwrap(); if (!reconnecting) { // It shouldn't be possible for Start to have already succeeded at this point. TryFailStart(exception); } else if (!_stop) { // Only raise the error event if the error wasn't raised from Start and we're reconnecting. connection.OnError(exception); Reconnect(connection, data, disconnectToken); } requestCancellationRegistration.Dispose(); } else { // If the disconnect token is canceled the response to the task doesn't matter. if (disconnectToken.IsCancellationRequested) { return; } var response = task.Result; Stream stream = response.GetStream(); var eventSource = new EventSourceStreamReader(connection, stream); eventSource.Opened = () => { // This will noop if we're not in the reconnecting state if (connection.ChangeState(ConnectionState.Reconnecting, ConnectionState.Connected)) { // Raise the reconnect event if the connection comes back up connection.OnReconnected(); } }; eventSource.Message = sseEvent => { if (sseEvent.EventType == EventType.Data && !sseEvent.Data.Equals("initialized", StringComparison.OrdinalIgnoreCase)) { ProcessResponse(connection, sseEvent.Data); } }; eventSource.Closed = exception => { // Make sure to try to fail start even if the disconnectToken tripped and set _stop to true. var startFailed = TryFailStart(exception); if (exception != null && !startFailed) { // Check if the request is aborted if (!ExceptionHelper.IsRequestAborted(exception)) { // Don't raise exceptions if the request was aborted (connection was stopped). connection.OnError(exception); } } requestCancellationRegistration.Dispose(); response.Dispose(); if (_stop) { AbortHandler.CompleteAbort(); } else if (!AbortHandler.TryCompleteAbort() && !startFailed) { // If Abort() was called or Start() failed, don't reconnect. Reconnect(connection, data, disconnectToken); } }; eventSource.Start(); } }); }
// For testing public virtual Task PerformConnect(CancellationToken token) { return(PerformConnect(UrlBuilder.BuildConnect(_connection, Name, _connectionData), token)); }