void internalHandler(object sender, MFCMessageEventArgs e) { var msg = e.Message; switch (msg.MessageType) { case MFCMessageType.FCTYPE_SESSIONSTATE: { if (_handleSessionState) onSessionState(msg); } break; case MFCMessageType.FCTYPE_LOGIN: { //when we get a login message, if it's a success message, save the session id if (msg.Arg1 == (int)MFCResponseType.FCRESPONSE_SUCCESS) { _loggedIn = true; _userId = msg.Arg2; _userName = msg.Data; _sessionId = msg.To.ToString(); } //not sure what to do here--for now, throw an exception if (msg.Arg1 == (int)MFCResponseType.FCRESPONSE_ERROR) throw new Exception("Unable to log in to MFC."); } break; case MFCMessageType.FCTYPE_METRICS: { //this is a little hairy but what we're trying to do is get an initial list //of models that are currently logged in to the server, primarily so we can //know what their broadcaster ids are when we want to join channels. //MFC sends a metrics message that contains a pointer to a javascript file //that has the currently logged in models right after you log in to the chat server. //They also send metrics messages that dont have the pointer, and I don't know what //those are for yet, so here we only work with the msg that matches the if statement //below. Then we strip out the javascript code so we're left with JSON we can //parse if (msg.To == 20 && msg.Arg1 == 0 && msg.Arg2 > 0) { //figure out the file pointer var fileno = JsonConvert.DeserializeObject<MetricsPayload>(WebUtility.UrlDecode(msg.Data)).fileno; //get the file var modelInfo = _client.DownloadString(String.Format(ModelUrlFormat, fileno)); Metrics = modelInfo; //strip out extraneous javascript modelInfo = modelInfo.Replace("var g_hModelData = ", ""); modelInfo = modelInfo.Replace("LoadModelsFromObject(g_hModelData);", ""); modelInfo = modelInfo.Replace("};", "}"); //attempt to deserialize the model JSON try { var m = JObject.Parse(modelInfo); //parse to a dynamic var n = m.Properties().Where(p => !p.Name.StartsWith("tags")) //filter out the tags objects .Children() //give us back JTokens .Select(x => JsonConvert.DeserializeObject<User>(x.ToString())); //serialize the individual model objects foreach (var model in n) updateUserInfo(model.UserId ?? default(int), model); } catch (Exception oops) { //eat any exceptions-we'll do without a model list if there's an issue //TODO: figure out something nicer to do here } } OnUsersProcessed(new EventArgs()); } break; } }
protected virtual void OnReceived(MFCMessageEventArgs e) { if (null != Received) Received(this, e); }