C# XMPP连接挂在“上”;资源绑定“;使用xmedianet库的进程
作为即时通讯(使用XMPP协议)以及windows phone 8.1应用程序开发领域的完全初学者;我试图从使用开始,以便连接到服务器并使用XMPP协议进行通信。在实现以下内容并根据我的需要进行调整之后。 下面是我配置连接参数的代码部分:C# XMPP连接挂在“上”;资源绑定“;使用xmedianet库的进程,c#,data-binding,windows-phone-8.1,xmpp,C#,Data Binding,Windows Phone 8.1,Xmpp,作为即时通讯(使用XMPP协议)以及windows phone 8.1应用程序开发领域的完全初学者;我试图从使用开始,以便连接到服务器并使用XMPP协议进行通信。在实现以下内容并根据我的需要进行调整之后。 下面是我配置连接参数的代码部分: using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Co
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using WP8Xmpp.Resources;
using System.Net.XMPP;
using System.Net.Sockets;
using System.Threading;
namespace WP8Xmpp
{
public partial class MainPage : PhoneApplicationPage
{
private Boolean IsXmppSuccess { get; set; }
/// <summary>
/// Xmpp Client
/// </summary>
public XMPPClient ObjXmppClient { get; set; }
/// <summary>
/// XMPP Connection
/// </summary>
public XMPPConnection ObjXmppCon { get; set; }
// Constructor
public MainPage()
{
InitializeComponent();
}
private void btnLogin_Click(object sender, RoutedEventArgs e)
{
IsXmppValid();
}
private void IsXmppValid()
{
ObjXmppClient = new XMPPClient();
//initializing the xmpp client with credentials
ObjXmppClient.JID = "user@domain.com";
ObjXmppClient.JID.Resource = Guid.NewGuid().ToString();
ObjXmppClient.Password = "acc_password";
ObjXmppClient.Server = "server_uri";*/
ObjXmppClient.AutoReconnect = true;
ObjXmppClient.Port = 81; // I've already tried 5222 but 81 is the correct port in this server's case.
ObjXmppClient.RetrieveRoster = true;
ObjXmppClient.PresenceStatus = new PresenceStatus() { PresenceType = PresenceType.available, IsOnline = true };
ObjXmppClient.AutoAcceptPresenceSubscribe = true;
ObjXmppClient.AttemptReconnectOnBadPing = true;
ObjXmppCon = new XMPPConnection(ObjXmppClient);
ObjXmppCon.Connect();
ObjXmppClient.Connect();
//initializing the xmpp connection
ObjXmppCon.OnAsyncConnectFinished += ObjXmppCon_OnAsyncConnectFinished;
ObjXmppClient.OnStateChanged += new EventHandler(xMPPClient_OnStateChanged);
Thread.Sleep(2000);
} ...
使用系统;
使用System.Collections.Generic;
使用System.Linq;
Net系统;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Navigation;
使用Microsoft.Phone.Controls;
使用Microsoft.Phone.Shell;
使用WP8Xmpp.Resources;
使用System.Net.XMPP;
使用System.Net.Sockets;
使用系统线程;
命名空间WP8Xmpp
{
公共部分类主页:PhoneApplicationPage
{
私有布尔值IsXmppSuccess{get;set;}
///
///Xmpp客户端
///
公共XMPPClient ObjXmppClient{get;set;}
///
///XMPP连接
///
公共XMPPConnection ObjXmppCon{get;set;}
//建造师
公共主页()
{
初始化组件();
}
私有void btnLogin\u单击(对象发送者,路由目标e)
{
IsXmppValid();
}
私有void IsXmppValid()
{
ObjXmppClient=新的XMPPClient();
//使用凭据初始化xmpp客户端
ObjXmppClient.JID=”user@domain.com";
ObjXmppClient.JID.Resource=Guid.NewGuid().ToString();
ObjXmppClient.Password=“acc_Password”;
ObjXmppClient.Server=“Server\u uri”*/
ObjXmppClient.AutoReconnect=true;
ObjXmppClient.Port=81;//我已经尝试了5222,但81是此服务器的正确端口。
ObjXmppClient.RetrieveRoster=true;
ObjXmppClient.PresenceStatus=new PresenceStatus(){PresenceType=PresenceType.available,IsOnline=true};
ObjXmppClient.AutoAcceptPresenceSubscribe=true;
ObjXmppClient.attemptReconnectonbapping=true;
ObjXmppCon=新的XMPPConnection(ObjXmppClient);
ObjXmppCon.Connect();
ObjXmppClient.Connect();
//初始化xmpp连接
ObjXmppCon.OnAsyncConnectFinished+=ObjXmppCon_OnAsyncConnectFinished;
ObjXmppClient.OnStateChanged+=新事件处理程序(xMPPClient\u OnStateChanged);
《睡眠》(2000年);
} ...
当我使用WP 8.1 emulator启动此应用程序并尝试连接时。在资源绑定步骤之前,一切正常。我在VS2013控制台上获得以下输出:
<--stream:features><ver xmlns="urn:xmpp:features:rosterver"/><compression xmlns="http://jabber.org/features/compress"><method>zlib</method></compression><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"/><session xmlns="urn:ietf:params:xml:ns:xmpp-session"/></stream:features>
Setting state to CanBind
Setting state to Binding
<--<
<--iq id="xxxxxxxx-xxxx-xxxx-xxxx-0f08b82b9f1f" to="user@domain/Resource" xmlns="jabber:client" type="result"><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"><jid>user@domain/Resource</jid></bind></iq>
zlib
将状态设置为CanBind
将状态设置为绑定
问题是,出于某种原因,“绑定”状态没有触发,即使绑定尝试返回了成功的结果。我通过使用Thread.sleep()并手动将状态更改为“绑定”(请注意,使用相同的技巧进入“会话”状态)成功地“修复”了这一问题。下面是我的示例“拼凑”代码:
void xMPPClient_OnStateChanged(object sender, EventArgs e)
{
switch (ObjXmppClient.XMPPState)
{
case XMPPState.Binding:
this.Dispatcher.BeginInvoke(() =>
{
Thread.Sleep(2000);
ObjXmppClient.XMPPState = System.Net.XMPP.XMPPState.Bound;
}
);
break;
case XMPPState.Sessioning:
this.Dispatcher.BeginInvoke(() =>
{
Thread.Sleep(2000);
ObjXmppClient.XMPPState = System.Net.XMPP.XMPPState.Session;
}
);
break; ...