我正在尝试在C#中为UWP实现XMPP客户端,为实现此目的,我使用StreamSocket
进行TCP
连接。
问题是握手和SASL auth
我需要服务器从客户端“写入和读取”序列,没有延迟我无法读取服务器发送的所有数据;我想删除延迟部分。
这是我的代码:
// Start handshake
await XmppWrite("<?xml version='1.0'?><stream:stream to='" + serverHostName.Domain + "' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>");
var responseStream = await XmppRead();
SaslPlain saslPlain = new SaslPlain(Username, Password);
responseStream = null;
String xml = null;
while (!saslPlain.IsCompleted && (xml = saslPlain.GetNextXml(responseStream)) != null)
{
await XmppWrite(xml);
responseStream = await XmppRead();
}
if (!saslPlain.IsSuccess) return;
// Request bind
await XmppWrite("<?xml version='1.0'?><stream:stream to='" + serverHostName.Domain + "' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>");
responseStream = await XmppRead();
// Bind
await XmppWrite("<iq id='" + GenerateId() + "' type='set'><bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/></iq>");
responseStream = await XmppRead();
if (responseStream.Jid == null) return;
Jid = responseStream.Jid;
// Open session
await XmppWrite("<iq id='" + GenerateId() + "' type='set'><session xmlns='urn:ietf:params:xml:ns:xmpp-session'/></iq>");
responseStream = await XmppRead();
XmppRead()方法是这样的:
private async Task<XmppStream> XmppRead()
{
await Task.Delay(100); // Things don't works without this...
var x = await dataReader.LoadAsync(BUFFER_SIZE);
if (dataReader.UnconsumedBufferLength > 0)
{
Debug.WriteLine(x + ", " + dataReader.UnconsumedBufferLength);
var stream = new XmppStream(dataReader.ReadString(dataReader.UnconsumedBufferLength));
if (stream.IsError)
{
if (Error != null)
{
Error(this, new XmppClientEventArgs(stream));
}
throw new Exception(stream.Error);
}
return stream;
}
return new XmppStream();
}
所以,我认为问题是我尝试在服务器完成之前读取所有数据,因此我只能读取部分消息。 如何检查是否有其他数据没有阻止?
感谢。