为什么所有的twisted/wokkel xmpp示例都会忽视xmpp协议中JID的正确用法?
好吧,这不是一个问题。我看到的关于wokkel和twisted的例子都没有正确处理JID中生成的资源。
使用wokkel/twisted构建的Google Talk客户端通常会出现问题,因为它们在响应时没有设置完整的JID,这会导致一些很隐蔽、很低级的错误,比如:
<message to="example@gmail.com" from="example2@synthasite.com/Example2C2F32A1" type="error"><body>echo: None</body><error code="400" type="modify"><bad-request xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/><text xmlns="urn:ietf:params:xml:ns:xmpp-stanzas">如果设置了,'from'属性必须设置为用户的完整JID。</text></error></message>
完整的、由服务器发出的JID可以通过协议处理器中的self.parent.authenticator.jid.full()获取。
所以,在发送消息时,确保在你的from字段中使用完整的JID,否则有些服务器会不喜欢你,那样你可能会抓狂,甚至想哭。
2 个回答
0
根据我的实验,应该用的是 self.parent.jid.full()
,而不是 self.parent.authenticator.jid.full()
。下面是代码片段
reply = toResponse(msg, msg.getAttribute('type'))
reply.addElement('body', content=unicode(msg.body))
reply["from"] = self.parent.jid.full()
self.send(reply)
5
在这个特定的情况下,最好的建议是你根本不需要设置发件人的地址。每个服务器都会很乐意帮你填好这个空白,这样你就不用去弄清楚你的客户端是什么JID了。