private void WebSocketOnMessageReceived(byte[] data, WebSocketWrapper _webSocket) { string dataText = CryptoUtility.UTF8EncodingNoPrefix.GetString(data); connection.Trace(TraceLevels.Messages, "WS: OnMessage({0})", dataText); ProcessResponse(connection, dataText); }
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); }
/// <summary> /// Connect a web socket to a path on the API and start listening, not all exchanges support this /// </summary> /// <param name="url">The sub url for the web socket, or null for none</param> /// <param name="messageCallback">Callback for messages</param> /// <param name="connectCallback">Connect callback</param> /// <returns>Web socket - dispose of the wrapper to shutdown the socket</returns> public WebSocketWrapper ConnectWebSocket(string url, Action <byte[], WebSocketWrapper> messageCallback, Action <WebSocketWrapper> connectCallback = null) { string fullUrl = BaseUrlWebSocket + (url ?? string.Empty); WebSocketWrapper wrapper = new WebSocketWrapper { Uri = new Uri(fullUrl), OnMessage = messageCallback, KeepAlive = TimeSpan.FromSeconds(5.0) }; if (connectCallback != null) { wrapper.Connected += (s) => connectCallback(wrapper); } wrapper.Start(); return(wrapper); }
private void DisposeWebSocket() { WebSocket.Dispose(); WebSocket = null; }
public WebsocketCustomTransport(IHttpClient client) : base(client, "webSockets") { WebSocket = new WebSocketWrapper(); }