private bool ProcessPusherChannelEvent(string eventName, string channelName, string messageData) { bool processed = true; switch (eventName) { case Constants.CHANNEL_SUBSCRIPTION_SUCCEEDED: _pusher.SubscriptionSuceeded(channelName, messageData); break; case Constants.CHANNEL_SUBSCRIPTION_ERROR: _pusher.SubscriptionFailed(channelName, messageData); break; case Constants.CHANNEL_MEMBER_ADDED: _pusher.AddMember(channelName, messageData); break; case Constants.CHANNEL_MEMBER_REMOVED: _pusher.RemoveMember(channelName, messageData); break; default: processed = false; break; } return(processed); }
private void websocket_MessageReceived(object sender, MessageReceivedEventArgs e) { Pusher.Trace.TraceEvent(TraceEventType.Information, 0, "Websocket message received: " + e.Message); Debug.WriteLine(e.Message); // DeserializeAnonymousType will throw and error when an error comes back from pusher // It stems from the fact that the data object is a string normally except when an error is sent back // then it's an object. // bad: "{\"event\":\"pusher:error\",\"data\":{\"code\":4201,\"message\":\"Pong reply not received\"}}" // good: "{\"event\":\"pusher:error\",\"data\":\"{\\\"code\\\":4201,\\\"message\\\":\\\"Pong reply not received\\\"}\"}"; var jObject = JObject.Parse(e.Message); if (jObject["data"] != null && jObject["data"].Type != JTokenType.String) { jObject["data"] = jObject["data"].ToString(Formatting.None); } var jsonMessage = jObject.ToString(Formatting.None); var template = new { @event = string.Empty, data = string.Empty, channel = string.Empty }; var message = JsonConvert.DeserializeAnonymousType(jsonMessage, template); var eventData = JsonConvert.DeserializeObject <Dictionary <string, object> >(jsonMessage); if (jObject["data"] != null) { eventData["data"] = jObject["data"].ToString(); // undo any kind of deserialisation of the data property } var receivedEvent = new PusherEvent(eventData, jsonMessage); _pusher.EmitPusherEvent(message.@event, receivedEvent); if ([email protected](Constants.PUSHER_MESSAGE_PREFIX)) { // Assume Pusher event switch (message.@event) { // TODO - Need to handle Error on subscribing to a channel case Constants.ERROR: ParseError(message.data); break; case Constants.CONNECTION_ESTABLISHED: ParseConnectionEstablished(message.data); break; case Constants.CHANNEL_SUBSCRIPTION_SUCCEEDED: _pusher.SubscriptionSuceeded(message.channel, message.data); break; case Constants.CHANNEL_SUBSCRIPTION_ERROR: RaiseError(new PusherException("Error received on channel subscriptions: " + e.Message, ErrorCodes.SubscriptionError)); break; case Constants.CHANNEL_MEMBER_ADDED: _pusher.AddMember(message.channel, message.data); Pusher.Trace.TraceEvent(TraceEventType.Warning, 0, "Received a presence event on channel '" + message.channel + "', however there is no presence channel which matches."); break; case Constants.CHANNEL_MEMBER_REMOVED: _pusher.RemoveMember(message.channel, message.data); Pusher.Trace.TraceEvent(TraceEventType.Warning, 0, "Received a presence event on channel '" + message.channel + "', however there is no presence channel which matches."); break; } } else // Assume channel event { _pusher.EmitChannelEvent(message.channel, message.@event, receivedEvent); } }