客户端与gtalk server建立会话的过程如下(默认中间没有错误发生):
1. ( C->S ) 连接到服务器, 发送'hello'信息.
<stream:stream to="gmail.com" version="1.0" xmlns:stream="
http://etherx.jabber.org/streams" xmlns="jabber:client">
2. ( S->C ) 服务器回应'hello'信息
<?xml version="1.0" encoding="UTF-8"?>
<stream:stream from="gmail.com" id="A9D1B4DB24EA879C" version="1.0" xmlns:stream="
http://etherx.jabber.org/streams" xmlns="jabber:client">
3. ( S->C ) 服务器要求TLS, SASL
<stream:features>
<starttls xmlns="urn:ietf:params:xml:ns:xmpp-tls"><required/></starttls>
<mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl">
<mechanism>X-GOOGLE-TOKEN</mechanism>
</mechanisms>
</stream:features>
4. ( C->S ) 开始TLS
<starttls xmlns="urn:ietf:params:xml:ns:xmpp-tls"/>
5. ( S->C ) 服务器允许继续TLS
<proceed xmlns="urn:ietf:params:xml:ns:xmpp-tls"/>
6. ( C->S ) TLS握手
客户端向服务器发起TLS握手请求, 如果握手成功, 执行#7 (可以使用openssl实现TLS的握手功能)
7. ( C->S ) TLS握手结束后, 发送新的'hello'消息
<stream:stream to="gmail.com" version="1.0" xmlns:stream="
http://etherx.jabber.org/streams" xmlns="jabber:client">
8. ( S->C ) 服务器回应'hello'消息
<?xml version="1.0" encoding="UTF-8"?>
<stream:stream from="gmail.com" id="D38877BD862E0EE4" version="1.0" xmlns:stream="
http://etherx.jabber.org/streams" xmlns="jabber:client">
9. ( S->C ) 服务器要求SASL
<stream:features>
<mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl">
<mechanism>PLAIN</mechanism>
<mechanism>X-GOOGLE-TOKEN</mechanism>
</mechanisms>
</stream:features>
10. ( C->S ) 客户端执行SASL
<auth xmlns="urn:ietf:params:xml:ns:xmpp-sasl" mechanism="PLAIN">
AHlzb25nLmxlZUBnbWFpbC5jb20AeXNvbmdAMTk4NA==
</auth>
auth的cdata的格式 '\0'+username+'\0'+password 的base64编码, username和password必须是经过认证的.
例如:
'\0' +
'ysong.lee@gmail.com' + '\0' + 123456 -> 经过base64编码处理后变为cdata
11. ( S->C ) SASL成功
<success xmlns="urn:ietf:params:xml:ns:xmpp-sasl"/>
12. ( C->S ) 客户端发送新的'hello'消息
<stream:stream to="gmail.com" version="1.0" xmlns:stream="
http://etherx.jabber.org/streams" xmlns="jabber:client">
13. ( S->C ) 服务器回应'hello'消息
<?xml version="1.0" encoding="UTF-8"?>
<stream:stream from="gmail.com" id="00035A2B998BF4B9" version="1.0" xmlns:stream="
http://etherx.jabber.org/streams" xmlns="jabber:client">
14. ( S->C ) 服务器要求绑定资源和建立一个会话
<stream:features>
<bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"/>
<session xmlns="urn:ietf:params:xml:ns:xmpp-session"/>
</stream:features>
15. ( C->S ) 客户端绑定申请一个资源
<iq type="set" id="1"><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"/></iq>
16. ( S->C ) 服务器返回绑定资源的结果
<iq id="1" type="result">
<bind xmlns="urn:ietf:params:xml:ns:xmpp-bind">
<jid>ysong.lee@gmail.com/BC20B630</jid>
</bind>
</iq>
17. ( C->S ) 客户端申请建立会话
<iq type="set" id="2">
<session xmlns="urn:ietf:params:xml:ns:xmpp-session"/>
</iq>
18. ( S->C ) 服务器返回建立会话成功
<iq type="result" id="2"/>
到此, Client和Gtalk server的一个会话已经建立, 可以根据jabber协议进行其它操作. 请求联系人列表, 发消息等.