public static Stanza GetStanza(XmlNode xml) { Stanza s = null; switch(xml.Name) { case "iq": s = new IQStanza(xml); break; case "message": s = new MessageStanza(xml); break; case "presence": s = new PresenceStanza(xml); break; default: s = new Stanza(xml); break; } return s; }
private void session_OnStanzaReceive(object sender, Stanza s) { if(InvokeRequired) { BeginInvoke(new sesson_OnStanzaReceiveDelegate(session_OnStanzaReceive), new object[] {sender, s}); return; } switch(s.Name) { case "message": onMessageStanzaReceived(s as MessageStanza); break; case "presence": onPresenceStanzaReceived(s as PresenceStanza); break; case "iq": onIqStanzaReceived(s as IQStanza); break; } }
/// <summary> /// Called when a new stanza is parsed from the stream. /// </summary> /// <param name="stanza">The stanza associated with the event.</param> private void onNewStanza(Stanza stanza) { // Handle resource binding here, but everything else is the application's responsibility if(state == SessionState.StartingSession && stanza is IQStanza) { IQStanza iq = (IQStanza)stanza; if(iq.Type == "result") { if(iq.Query != null && iq.Query.Name == "bind") { // Start a session with the given resource name startSession(iq.Query.FirstChild.InnerText); } else if(iq.Query == null || (iq.Query != null && iq.Query.Name == "session")) { // Bind has been confirmed so we are now logged in setState(SessionState.LoggedIn); return; } } } else if(state == SessionState.LoggedIn) { if(OnStanzaReceive != null) OnStanzaReceive(this, stanza); } else { if(OnError != null) OnError(this, "Stanza "+stanza+" was received in the "+state+" state"); } }
/// <summary> /// Sends a stanza asynchronously. /// </summary> /// <param name="s">Stanza to send</param> public void SendStanza(Stanza s) { // Don't send unless we are logged in if(state != SessionState.LoggedIn) { OpenXMPPException err = new OpenXMPPException("Tried to send stanza \""+s+"\" in the "+state+" state."); if(OnError != null) OnError(this, err); } send(s); // Raise event if(OnStanzaSend != null) OnStanzaSend(this, s); }