带有StremSocket的C#XMPP在资源绑定后挂起

本文关键字:绑定 挂起 资源 StremSocket C#XMPP 带有 | 更新日期: 2023-09-27 18:26:16

我需要用C#为UWP应用程序构建一个XMPP客户端。

我知道有一些.NET库,但似乎运行得不太好(那些是免费的),所以我决定自己实现它。

服务器端是一个ejabberd服务。

我以这种方式使用StreamSocket(参考XMPP 3920bis):

            StreamSocket socket = new StreamSocket();
            await socket.ConnectAsync(new HostName("my.server.com"), "5222", SocketProtectionLevel.PlainSocket);
            DataWriter dw = new DataWriter(socket.OutputStream);
            DataReader dr = new DataReader(socket.InputStream);
            dr.InputStreamOptions = InputStreamOptions.Partial;
            // Open stream
            await XmppWrite("<?xml version='1.0'?><stream:stream to='my.server.com' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>", dw);
            var resp = await XmppRead(dr);
            // Auth
            var sasl = Base64Encode(''0' + SaslPrep("username") + ''0' + SaslPrep("password"));
            await XmppWrite("<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='PLAIN'>" + sasl + "</auth>", dw);
            resp = await XmppRead(dr);
            // Request bind
            await XmppWrite("<?xml version='1.0'?><stream:stream to='my.server.com' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>", dw);
            resp = await XmppRead(dr);
            // Resource binding
            await XmppWrite("<iq id='"+ObtainID()+"' type='set'><bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/></iq>", dw);
            resp = await XmppRead(dr);
            // Send initial presence
            await XmppWrite("<presence/>", dw);
            resp = await XmppRead(dr);

一切都很好,直到我发送了初始状态,服务器没有响应,连接关闭。

有什么想法吗?

带有StremSocket的C#XMPP在资源绑定后挂起

问题出现在资源绑定后缺少的步骤"打开会话"中。

// Resource binding
await XmppWrite("<iq id='"+ObtainID()+"' type='set'><bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/></iq>", dw);
resp = await XmppRead(dr);
// Open session
await XmppWrite("<iq id='" + Guid.NewGuid() + "' type='set'><session xmlns='urn:ietf:params:xml:ns:xmpp-session'/></iq>", dw);
resp = await XmppRead(dr);
// Send initial presence
await XmppWrite("<presence/>", dw);
resp = await XmppRead(dr);