private async Task <Iq> DoBindAsync(StreamFeatures features, CancellationToken cancellationToken) { XmppSessionState.Value = SessionState.Binding; var bIq = new BindIq { Type = IqType.Set, Bind = { Resource = Resource } }; var resBindIq = await SendIqAsync(bIq, cancellationToken); if (resBindIq.Type != IqType.Result) { throw new BindException(resBindIq); } if (features.SupportsSession && !features.Session.Optional) { var sessionIq = new SessionIq { Type = IqType.Set }; var resSessionIq = await SendIqAsync(sessionIq, cancellationToken); } XmppSessionState.Value = SessionState.Binded; return(resBindIq); }
/// <summary> /// 处理绑定返回的IQ消息 /// </summary> /// <param name="sender"></param> /// <param name="iq"></param> /// <param name="data"></param> private void BindResult(object sender, IQ iq, object data) { // Once the server has generated a resource identifier for the client or accepted the resource // identifier provided by the client, it MUST return an IQ stanza of type "result" // to the client, which MUST include a <jid/> child element that specifies the full JID for // the connected resource as determined by the server: // Server informs client of successful resource binding: // <iq type='result' id='bind_2'> // <bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'> // <jid>[email protected]/someresource</jid> // </bind> // </iq> if (iq.Type == IqType.result) { // i assume the server could assign another resource here to the client // so grep the resource assigned by the server now Element bind = iq.SelectSingleElement(typeof(Bind)); if (bind != null) { Jid jid = ((Bind)bind).Jid; Resource = jid.Resource; //UserName = jid.User; } // 已经绑定 DoChangeXmppConnectionState(XmppConnectionState.Binded); Binded = true; //DoRaiseEventBinded(); // 绑定成功后,可以为开始会话状态 DoChangeXmppConnectionState(XmppConnectionState.StartSession); SessionIq sIq = new SessionIq(IqType.set, new Jid(Server)); // 发送会话请求IQ this.IqGrabber.SendIq(sIq, SessionResult, null); } else if (iq.Type == IqType.error) { if (OnError != null) { OnError(this, new Exception("绑定请求失败")); } } }
private void BindResult(object sender, IQEventArgs e) { var iq = e.IQ; // Once the server has generated a resource identifier for the client or accepted the resource // identifier provided by the client, it MUST return an IQ stanza of type "result" // to the client, which MUST include a <jid/> child element that specifies the full JID for // the connected resource as determined by the server: // Server informs client of successful resource binding: // <iq type='result' id='bind_2'> // <bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'> // <jid>[email protected]/someresource</jid> // </bind> // </iq> if (iq.Type == IqType.result) { // i assume the server could assign another resource here to the client // so grep the resource assigned by the server now Element bind = iq.SelectSingleElement(typeof(Bind)); if (bind != null) { Jid jid = ((Bind)bind).Jid; m_XmppClient.Resource = jid.Resource; m_XmppClient.Username = jid.User; } m_XmppClient.DoChangeXmppConnectionState(XmppConnectionState.Binded); m_XmppClient.m_Binded = true; m_XmppClient.DoRaiseEventBinded(); // success, so start the session now m_XmppClient.DoChangeXmppConnectionState(XmppConnectionState.StartSession); SessionIq sIq = new SessionIq(IqType.set, new Jid(m_XmppClient.Server)); m_XmppClient.IqGrabber.SendIq(sIq, SessionResult); e.Handled = true; } else if (iq.Type == IqType.error) { // TODO, handle bind errors m_XmppClient.DoRaiseEventBindError(iq); } }
/// <summary> /// </summary> /// <param name="sender"> /// </param> /// <param name="iq"> /// </param> /// <param name="data"> /// </param> private void BindResult(object sender, IQ iq, object data) { // Once the server has generated a resource identifier for the client or accepted the resource // identifier provided by the client, it MUST return an IQ stanza of type "result" // to the client, which MUST include a <jid/> child element that specifies the full JID for // the connected resource as determined by the server: // Server informs client of successful resource binding: // <iq type='result' id='bind_2'> // <bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'> // <jid>[email protected]/someresource</jid> // </bind> // </iq> if (iq.Type == IqType.result) { // i assume the server could assign another resource here to the client // so grep the resource assigned by the server now Element bind = iq.SelectSingleElement(typeof (Bind)); if (bind != null) { Jid jid = ((Bind) bind).Jid; m_XmppClient.Resource = jid.Resource; m_XmppClient.Username = jid.User; } m_XmppClient.DoChangeXmppConnectionState(XmppConnectionState.Binded); m_XmppClient.m_Binded = true; m_XmppClient.DoRaiseEventBinded(); // success, so start the session now m_XmppClient.DoChangeXmppConnectionState(XmppConnectionState.StartSession); SessionIq sIq = new SessionIq(IqType.set, new Jid(m_XmppClient.Server)); m_XmppClient.IqGrabber.SendIq(sIq, new IqCB(SessionResult), null); } else if (iq.Type == IqType.error) { // TODO, handle bind errors } }