private void Connect(IWebSocket webSocket) { var connectedEvent = new ManualResetEvent(false); EventHandler onOpenAction = (s, e) => { connectedEvent.Set(); }; webSocket.Open += onOpenAction; try { webSocket.Connect(); if (!connectedEvent.WaitOne(ConnectionTimeout)) { throw new Exception("BitfinexSubscriptionManager.Connect(): WebSocket connection timeout."); } } finally { webSocket.Open -= onOpenAction; connectedEvent.DisposeSafely(); } }
/// <summary> /// Establish websocket connection /// </summary> /// <param name="url">url of the websocket server</param> /// <param name="authorizationToken">user authorization token</param> /// <param name="sessionId">session id header</param> public void Connect(string url, string authorizationToken, string sessionId = "") { if (State == WsState.Connecting || State == WsState.Open) { AccelByteDebug.Log("[AccelByteWebSocket] is connecting or already connected"); return; } if (this.tokenGenerator != null) { if (!this.tokenGenerator.IsValid()) { this.tokenGenerator.RequestToken(); return; } } webSocketUrl = url; this.authorizationToken = authorizationToken; webSocket.Connect(url, this.authorizationToken, sessionId, this.tokenGenerator?.Token); // check status after connect, only maintain connection when close code is reconnectable if (this.closeCodeCurrent == WsCloseCode.NotSet || isReconnectable(this.closeCodeCurrent)) { StartMaintainConnection(); } }
/// <summary> /// Start a WebSocket connection /// </summary> public void Connect() { _timerTick = _interval; _timer.Start(); if (!IsConnected) { _ws.Connect(_socketUrl, new Dictionary <string, string>() { ["X-Kite-Version"] = "3" }); } }
public void Connect(string url) { if (mSocketClient != null) { throw new Exception("Please disconnect the connected/connecting WebScoket."); } mHost = url; mSocketClient = WebSocketFactory.CreateInstance(url); mSocketClient.OnOpen += OnConnected; mSocketClient.OnClose += OnDisconnected; mSocketClient.OnMessage += OnMessage; mSocketClient.OnError += OnError; mSocketClient.Connect(); }
private void ConnectSync() { var resetEvent = new ManualResetEvent(false); EventHandler triggerEvent = (o, args) => resetEvent.Set(); WebSocket.Open += triggerEvent; WebSocket.Connect(); if (!resetEvent.WaitOne(ConnectionTimeout)) { throw new TimeoutException("Websockets connection timeout."); } WebSocket.Open -= triggerEvent; }
public TickerTape(string accessToken) { Token = accessToken; if (_ws == null) { _ws = new WebSocket(); } _ws.OnConnect += _ws_OnConnect; _ws.OnData += _ws_OnData; _ws.OnClose += _ws_OnClose; _ws.OnError += _ws_OnError; string url = string.Format(base_url, Token); _ws.Connect(url); }
/// <summary> /// Start a WebSocket connection /// </summary> public void Connect() { _apiKey = configuration.GetValue <string>("kiteApiKey"); KiteAccessTokenResponseRoot kiteAccessTokenResponseRoot; bool cacheFetchResult = memoryCache.TryGetValue <KiteAccessTokenResponseRoot>("kite_access_token", out kiteAccessTokenResponseRoot); if (cacheFetchResult) { _accessToken = kiteAccessTokenResponseRoot.data.access_token; } _socketUrl = _root + String.Format("?api_key={0}&access_token={1}", _apiKey, _accessToken); _timerTick = _interval; _timer.Start(); if (!IsConnected) { _ws.Connect(_socketUrl, new Dictionary <string, string>() { ["X-Kite-Version"] = "3" }); } }
/// <summary> /// Creates wss connection, monitors for disconnection and re-connects when necessary /// </summary> public override void Connect() { if (IsConnected) { return; } Log.Trace("BaseWebSocketsBrokerage.Connect(): Connecting..."); WebSocket.Connect(); Wait(_connectionTimeout, () => WebSocket.IsOpen); _cancellationTokenSource = new CancellationTokenSource(); _connectionMonitorThread = new Thread(() => { var nextReconnectionAttemptUtcTime = DateTime.UtcNow; double nextReconnectionAttemptSeconds = 1; lock (_lockerConnectionMonitor) { LastHeartbeatUtcTime = DateTime.UtcNow; } try { while (!_cancellationTokenSource.IsCancellationRequested) { if (WebSocket.IsOpen) { LastHeartbeatUtcTime = DateTime.UtcNow; } TimeSpan elapsed; lock (_lockerConnectionMonitor) { elapsed = DateTime.UtcNow - LastHeartbeatUtcTime; } if (!_connectionLost && elapsed > TimeSpan.FromSeconds(_heartbeatTimeout)) { if (WebSocket.IsOpen) { // connection is still good LastHeartbeatUtcTime = DateTime.UtcNow; } else { _connectionLost = true; nextReconnectionAttemptUtcTime = DateTime.UtcNow.AddSeconds(nextReconnectionAttemptSeconds); OnMessage(BrokerageMessageEvent.Disconnected("Connection with server lost. This could be because of internet connectivity issues.")); } } else if (_connectionLost) { try { if (elapsed <= TimeSpan.FromSeconds(_heartbeatTimeout)) { _connectionLost = false; nextReconnectionAttemptSeconds = 1; OnMessage(BrokerageMessageEvent.Reconnected("Connection with server restored.")); } else { if (DateTime.UtcNow > nextReconnectionAttemptUtcTime) { try { Reconnect(); } catch (Exception err) { // double the interval between attempts (capped to 1 minute) nextReconnectionAttemptSeconds = Math.Min(nextReconnectionAttemptSeconds * 2, 60); nextReconnectionAttemptUtcTime = DateTime.UtcNow.AddSeconds(nextReconnectionAttemptSeconds); Log.Error(err); } } } } catch (Exception exception) { Log.Error(exception); } } Thread.Sleep(10000); } } catch (Exception exception) { Log.Error(exception); } }) { IsBackground = true }; _connectionMonitorThread.Start(); while (!_connectionMonitorThread.IsAlive) { Thread.Sleep(1); } }
public IEnumerator Connect(GamebaseCallback.ErrorDelegate callback) { yield return(GamebaseUnitySDKSettings.Instance.StartCoroutine(socket.Connect(callback))); }
/// <summary> /// Opens connection to a streaming API. /// </summary> public void Connect() => _webSocket.Connect();
public IEnumerator Connect(GamebaseCallback.ErrorDelegate callback) { yield return(GamebaseCoroutineManager.StartCoroutine(GamebaseGameObjectManager.GameObjectType.WEBSOCKET_TYPE, socket.Connect(callback))); }
public void Connect() { _websocket.Connect(); _websocket.OnMessage += OnMessage; Authenticate(); }