public static T ReadAsProtoJson <T>(this HttpContent content) where T : new() { JArray arrayBody = JArray.Parse(content.ReadAsStringAsync().Result); arrayBody.RemoveAt(0); return(ProtoJsonSerializer.Deserialize <T>(arrayBody)); }
public static T ReadAsProtoJson <T>(this HttpContent content) where T : new() { using (System.IO.Stream stream = content.ReadAsStreamAsync().Result) { var reader = new Newtonsoft.Json.JsonTextReader(new System.IO.StreamReader(stream)); var arrayBody = JArray.Load(reader); arrayBody.RemoveAt(0); return(ProtoJsonSerializer.Deserialize <T>(arrayBody)); } }
public async static Task <T> ReadAsProtoJson <T>(this HttpContent httpContent) where T : new() { //using (System.IO.Stream stream = await content.ReadAsStreamAsync()) //{ // var reader = new Newtonsoft.Json.JsonTextReader(new System.IO.StreamReader(stream)); // var arrayBody = JArray.Load(reader); // arrayBody.RemoveAt(0); // return ProtoJsonSerializer.Deserialize<T>(arrayBody); //} System.IO.Stream content = await httpContent.ReadAsStreamAsync(); var arrayBody = new DynamicJson(content); arrayBody.RemoveAt(0); return(ProtoJsonSerializer.Deserialize <T>(arrayBody)); }
async void _channel_OnDataReceived(object sender, DynamicJson rawdata) { //Parse channel array and call the appropriate events. if (rawdata[0].ToString() == "noop") { // set active client if more than 120 sec of inactivity if ((DateTime.UtcNow - _last_response_date).TotalSeconds > 120 && _client_id != null) { SetActiveClientAsync(); _last_response_date = DateTime.UtcNow; } } else if (rawdata[0].ToString() == "resync") // internal handling { SyncAllNewEventsAsync(long.Parse(rawdata[1].ToString())); } else if (rawdata[0]["p"] != null) { DynamicJson wrapper = new DynamicJson(rawdata[0]["p"].ToString()); if (wrapper["4"] != null && wrapper["4"]["2"] != null) { if (_channel.Connected && !_wasConnected) { _client_id = wrapper["4"]["2"].ToString(); _requestHeader = null; _channel.SendAck(0); _wasConnected = _channel.Connected; // load self info await GetSelfInfoAsync(); // load conversations if not loaded await SyncRecentConversationsAsync(); if (OnConnectionEstablished != null) { OnConnectionEstablished(this, null); } } } if (wrapper["2"] != null && wrapper["2"]["2"] != null) { DynamicJson cbu = new DynamicJson(wrapper["2"]["2"].ToString()); cbu.RemoveAt(0); BatchUpdate batchUpdate = ProtoJsonSerializer.Deserialize <BatchUpdate>(cbu as DynamicJson); foreach (StateUpdate state_update in batchUpdate.state_update) { if (state_update.event_notification != null) { switch (state_update.event_notification.current_event.event_type) { case EventType.EVENT_TYPE_REGULAR_CHAT_MESSAGE: case EventType.EVENT_TYPE_UNKNOWN: if (state_update.event_notification.current_event.conversation_id == null) { break; } if (_active_conversations.ContainsKey(state_update.event_notification.current_event.conversation_id.id)) { _active_conversations[state_update.event_notification.current_event.conversation_id.id].events.Add(state_update.event_notification.current_event); if (OnNewMessageReceived != null) { OnNewMessageReceived(this, state_update.event_notification.current_event); } } else { // get conversation await GetConversationAsync(state_update.event_notification.current_event.conversation_id.id); //ConversationState s = new ConversationState() //{ // conversation_id = state_update.event_notification.current_event.conversation_id, // conversation = state_update.conversation, // events = new List<Event>() { state_update.event_notification.current_event } //}; //_active_conversations.Add(s.conversation_id.id, new Model.Conversation(s)); //if (NewConversationCreated != null) // NewConversationCreated(this, _active_conversations.Last().Value); } break; case EventType.EVENT_TYPE_OTR_MODIFICATION: _active_conversations[state_update.event_notification.current_event.conversation_id.id].conversation.otr_status = state_update.event_notification.current_event.otr_status; break; case EventType.EVENT_TYPE_CONVERSATION_RENAME: _active_conversations[state_update.event_notification.current_event.conversation_id.id].conversation.name = state_update.event_notification.current_event.conversation_rename.new_name; break; } } if (state_update.presence_notification != null) { foreach (var presence in state_update.presence_notification.presence) { if (_contacts.ContainsKey(presence.user_id.gaia_id)) { _contacts[presence.user_id.gaia_id].presence = presence.presence; } } } if (state_update.self_presence_notification != null) { CurrentUser.presence = new Presence() { available = state_update.self_presence_notification.client_presence_state.state == ClientPresenceStateType.CLIENT_PRESENCE_STATE_DESKTOP_ACTIVE } } ; if (state_update.watermark_notification != null) { if (state_update.watermark_notification.sender_id.gaia_id == CurrentUser.id.gaia_id) { _active_conversations[state_update.watermark_notification.conversation_id.id].conversation.self_conversation_state.self_read_state.latest_read_timestamp = state_update.watermark_notification.latest_read_timestamp; } //else // _active_conversations[state_update.watermark_notification.conversation_id.id].read_state. = state_update.watermark_notification.latest_read_timestamp.FromUnixTime(); } } _timestamp = long.Parse(wrapper["1"]["4"].ToString()); } } }
async void _channel_OnDataReceived(object sender, JArray rawdata) { //Parse channel array and call the appropriate events. if (rawdata[0].ToString() == "noop") { // set active client if more than 120 sec of inactivity if ((DateTime.UtcNow - _last_response_date).TotalSeconds > 120 && _client_id != null) { SetActiveClientAsync(); _last_response_date = DateTime.UtcNow; } } else if (rawdata[0].ToString() == "resync") { SyncAllNewEventsAsync(long.Parse(rawdata[1].ToString())); } else if (rawdata[0]["p"] != null) { JObject wrapper = JObject.Parse(rawdata[0]["p"].ToString()); if (wrapper["3"] != null && wrapper["3"]["2"] != null) { _client_id = wrapper["3"]["2"].ToString(); _requestHeader = null; _channel.SendAck(0); if (_channel.Connected && !_wasConnected) { _wasConnected = _channel.Connected; if (ConnectionEstablished != null) { ConnectionEstablished(this, null); } // check if contacts are loaded if (_contacts == null || _contacts.Count == 0) { if (ContactListLoaded != null) { ContactListLoaded(this, _contacts.Values.ToList()); } string[] participants_id = _active_conversations.Values.SelectMany(c => c._conversation.current_participant.Where(p => p.gaia_id != CurrentUser.Id).Select(p => p.gaia_id)).Distinct().ToArray(); await GetEntityByIdAsync(participants_id); if (ConversationHistoryLoaded != null) { ConversationHistoryLoaded(this, _active_conversations.Values.ToList()); } } } } if (wrapper["2"] != null && wrapper["2"]["2"] != null) { JArray cbu = JArray.Parse(wrapper["2"]["2"].ToString()); cbu.RemoveAt(0); BatchUpdate batchUpdate = ProtoJsonSerializer.Deserialize <BatchUpdate>(cbu as JArray); foreach (StateUpdate state_update in batchUpdate.state_update) { if (state_update.event_notification != null) { switch (state_update.event_notification.current_event.event_type) { case EventType.EVENT_TYPE_REGULAR_CHAT_MESSAGE: case EventType.EVENT_TYPE_UNKNOWN: if (state_update.event_notification.current_event.conversation_id == null) { break; } if (_active_conversations.ContainsKey(state_update.event_notification.current_event.conversation_id.id)) { _active_conversations[state_update.event_notification.current_event.conversation_id.id].AddNewMessage(state_update.event_notification.current_event); if (NewMessageReceived != null) { NewMessageReceived(this, _active_conversations[state_update.event_notification.current_event.conversation_id.id]); } } else { ConversationState s = new ConversationState() { conversation_id = state_update.event_notification.current_event.conversation_id, conversation = state_update.conversation, events = new List <Event>() { state_update.event_notification.current_event } }; _active_conversations.Add(s.conversation_id.id, new Model.Conversation(s)); if (NewConversationCreated != null) { NewConversationCreated(this, _active_conversations.Last().Value); } } break; case EventType.EVENT_TYPE_OTR_MODIFICATION: _active_conversations[state_update.event_notification.current_event.conversation_id.id]._conversation.otr_status = state_update.event_notification.current_event.otr_status; break; case EventType.EVENT_TYPE_CONVERSATION_RENAME: _active_conversations[state_update.event_notification.current_event.conversation_id.id]._conversation.name = state_update.event_notification.current_event.conversation_rename.new_name; break; } } if (state_update.presence_notification != null) { foreach (var presence in state_update.presence_notification.presence) { if (_contacts.ContainsKey(presence.user_id.gaia_id)) { setPresence(_contacts[presence.user_id.gaia_id], presence.presence); } } } if (state_update.self_presence_notification != null) { CurrentUser.SetPresence(new Presence() { available = state_update.self_presence_notification.client_presence_state.state == ClientPresenceStateType.CLIENT_PRESENCE_STATE_DESKTOP_ACTIVE } ); } if (state_update.watermark_notification != null) { if (state_update.watermark_notification.sender_id.gaia_id == CurrentUser.Id) { _active_conversations[state_update.watermark_notification.conversation_id.id].SelfReadState = state_update.watermark_notification.latest_read_timestamp.FromUnixTime(); } else { _active_conversations[state_update.watermark_notification.conversation_id.id].ReadState = state_update.watermark_notification.latest_read_timestamp.FromUnixTime(); } } } _timestamp = long.Parse(wrapper["1"]["4"].ToString()); } } }
async Task initializeChat() { // We first need to fetch the 'pvt' token, which is required for the // initialization request (otherwise it will return 400). HttpResponseMessage message = await _client.Execute(HangoutUri.PVT_TOKEN_URL, _initParams); string data = await message.Content.ReadAsStringAsync(); Newtonsoft.Json.Linq.JArray array = JArray.Parse(data); _initParams["pvt"] = array[1].ToString(); // Now make the actual initialization request: message = await _client.Execute(HangoutUri.CHAT_INIT_URL, _initParams); data = await message.Content.ReadAsStringAsync(); message.Dispose(); // Parse the response by using a regex to find all the JS objects, and // parsing them. Not everything will be parsable, but we don't care if // an object we don't need can't be parsed. Dictionary <string, Newtonsoft.Json.Linq.JArray> dataDictionary = Parser.ParseInitParams(data); // a this time we can not fix key as index can change. string key = null; foreach (JArray jarray in dataDictionary.Values) { try { key = jarray[0][0].ToString(); } catch { key = null; } switch (key) { case "cin:cac": _api_key = jarray[0][2].ToString(); break; case "cic:vd": _email = jarray[0][2].ToString(); // cic:vd break; case "cin:acc": if (jarray[0].Count() > 6) { _header_date = jarray[0][4].ToString(); //cin:acc _header_version = jarray[0][6].ToString(); } break; case "cin:bcsc": _header_id = jarray[0][7].ToString(); // cin:bcsc break; case "cgserp": jarray[0][0].Remove(); GetSuggestedEntitiesResponse cgserp = ProtoJsonSerializer.Deserialize <GetSuggestedEntitiesResponse>(jarray[0] as JArray); if (cgserp.response_header.status == ResponseStatus.RESPONSE_STATUS_OK) { _contacts = cgserp.contacts_you_hangout_with.contact.ToDictionary(c => c.entity.id.gaia_id, c => new User(c.entity)); } break; case "cgsirp": jarray[0][0].Remove(); GetSelfInfoResponse cgsirp = ProtoJsonSerializer.Deserialize <GetSelfInfoResponse>(jarray[0] as JArray); if (cgsirp.response_header.status == ResponseStatus.RESPONSE_STATUS_OK) { if (!string.IsNullOrEmpty(_email)) { cgsirp.self_entity.properties.canonical_email = _email; } CurrentUser = new User(cgsirp.self_entity); } break; case "csrcrp": jarray[0][0].Remove(); SyncRecentConversationsResponse csrcrp = ProtoJsonSerializer.Deserialize <SyncRecentConversationsResponse>(jarray[0] as JArray); if (csrcrp.response_header.status == ResponseStatus.RESPONSE_STATUS_OK) { _active_conversations = csrcrp.conversation_state.ToDictionary(c => c.conversation_id.id, c => new WTalk.Model.Conversation(c)); } break; } } // call all events if (UserInformationReceived != null) { UserInformationReceived(this, CurrentUser); } data = null; dataDictionary = null; //this._timestamp = double.Parse(dataDictionary["ds:21"][0][1][4].ToString()); }
public static HttpResponseMessage PostProtoJson <T>(this HttpClient client, string endPoint, T protoJsonObject) where T : class { JArray body = ProtoJsonSerializer.Serialize(protoJsonObject); return(client.execute(endPoint, body, false)); }
void _channel_OnDataReceived(object sender, JArray rawdata) { //Parse channel array and call the appropriate events. if (rawdata[0].ToString() == "noop") { // set active client if more than 120 sec of inactivity if ((DateTime.UtcNow - _last_response_date).TotalSeconds > 120) { SetActiveClient(); _last_response_date = DateTime.UtcNow; } } else if (rawdata[0]["p"] != null) { JObject wrapper = JObject.Parse(rawdata[0]["p"].ToString()); if (wrapper["3"] != null && wrapper["3"]["2"] != null) { _client_id = wrapper["3"]["2"].ToString(); _requestHeader = null; _channel.SendAck(0); if (_channel.Connected && !_wasConnected) { _wasConnected = _channel.Connected; if (ConnectionEstablished != null) { ConnectionEstablished(this, null); } } } if (wrapper["2"] != null && wrapper["2"]["2"] != null) { JArray cbu = JArray.Parse(wrapper["2"]["2"].ToString()); cbu.RemoveAt(0); BatchUpdate batchUpdate = ProtoJsonSerializer.Deserialize <BatchUpdate>(cbu as JArray); foreach (StateUpdate state_update in batchUpdate.state_update) { if (state_update.event_notification != null) { if (state_update.event_notification.current_event.event_type == EventType.EVENT_TYPE_REGULAR_CHAT_MESSAGE) { if (_active_conversation_ids.Contains(state_update.event_notification.current_event.conversation_id.id)) { if (NewConversationEventReceived != null) { NewConversationEventReceived(this, state_update.event_notification.current_event); } } else { if (NewConversationCreated != null) { ConversationState s = new ConversationState() { conversation_id = state_update.event_notification.current_event.conversation_id, conversation = state_update.conversation, events = new List <Event>() { state_update.event_notification.current_event } }; NewConversationCreated(this, s); } } } } if (state_update.presence_notification != null) { foreach (var presence in state_update.presence_notification.presence) { PresenceInformationReceived(this, presence); } } if (state_update.self_presence_notification != null) { PresenceInformationReceived(this, new PresenceResult() { user_id = CurrentUser.id, presence = new Presence() { available = state_update.self_presence_notification.client_presence_state.state == ClientPresenceStateType.CLIENT_PRESENCE_STATE_DESKTOP_ACTIVE } }); } } this._timestamp = long.Parse(wrapper["1"]["4"].ToString()); } } }
public async static Task <HttpResponseMessage> PostProtoJson <T>(this HttpClient client, string endPoint, string apiKey, T protoJsonObject) where T : class { JArray body = ProtoJsonSerializer.Serialize(protoJsonObject); return(await client.execute(endPoint, apiKey, body, false)); }