public Task <bool> ConnectAsync() { var tcs = new TaskCompletionSource <bool>(); if (_client == null) { InitializeClient(); } Action openedConnectionHandler = null; openedConnectionHandler = () => { Log("Websocket connection opened", 0); if (identifierString == "") { identifierString = GetIdentifierString(); } var connectRequest = new { command = "subscribe", identifier = identifierString }; string commandRequestString = JsonConvert.SerializeObject(connectRequest); Log("Subscribing to " + Channel, 1); Action <string> onMessageHandler = null; onMessageHandler = (obj) => { var e = new ActionCableEventArgs(obj); if (e.Type == "confirm_subscription" && e.Channel == Channel) { Log("Subscription success", 1); _client.OnMessage -= onMessageHandler; _client.OnOpened -= openedConnectionHandler; _client.OnMessage += HandleIncomingMessage; RaiseConnected(obj); tcs.SetResult(true); } }; _client.OnMessage += onMessageHandler; _client.Send(commandRequestString); }; _client.OnOpened += openedConnectionHandler; _client.Open(string.Format("{0}{1}{2}", RemoteUrl, Port > 0 ? ":" : "", Port > 0 ? Port.ToString() : "")); Log("Opening websocket connection", 0); return(tcs.Task); }
void HandleIncomingMessage(string obj) { //Log("Message received: " + obj, 0); var arg = new ActionCableEventArgs(obj); if (arg.Type == "ping" && IgnorePings) { return; } RaiseMessageReceived(obj); }