2

我尝试在 ruby​​ 中使用 xmpp4r 连接到我的 XMPP 服务器,我的代码非常简单,但我不知道为什么会出错。我可以成功使用 ichat 使用 chat@localhost 登录,也可以登录 5280/admin 网站。

require 'xmpp4r/client'
include Jabber
Jabber::debug = true
jid = Jabber::JID.new('chat@localhost')
client = Jabber::Client.new(jid)
client.connect
client.auth('123456')
client.close

错误是:

D, [2013-02-18T03:24:55.768029 #15238] DEBUG -- : Debugging mode enabled.
W, [2013-02-18T03:24:55.768341 #15238]  WARN -- : Warnings mode enabled.
D, [2013-02-18T03:24:55.772467 #15238] DEBUG -- : RESOLVING:
_xmpp-client._tcp.localhost (SRV)
D, [2013-02-18T03:24:55.778631 #15238] DEBUG -- : CONNECTING:
localhost:5222
D, [2013-02-18T03:24:55.780122 #15238] DEBUG -- : SENDING:
<stream:stream xmlns:stream='http://etherx.jabber.org/streams'     
xmlns='jabber:client' to='localhost' xml:lang='en' version='1.0' >
D, [2013-02-18T03:24:55.782276 #15238] DEBUG -- : RECEIVED:
<stream:stream from='localhost' id='3021975152' xml:lang='en' 
xmlns:stream='http://etherx.jabber.org/streams' version='1.0'     
xmlns='jabber:client'/>
D, [2013-02-18T03:24:55.784022 #15238] DEBUG -- : RECEIVED:
<stream:features><mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>    
<mechanism>SCRAM-SHA-1</mechanism><mechanism>DIGEST-MD5</mechanism>
<mechanism>PLAIN</mechanism></mechanisms><c hash='sha-1' 
node='http://www.process-one.net/en/ejabberd/' 
ver='yy7di5kE0syuCXOQTXNBTclpNTo=' xmlns='http://jabber.org/protocol/caps'/>
<register xmlns='http://jabber.org/features/iq-register'/></stream:features>
D, [2013-02-18T03:24:55.784203 #15238] DEBUG -- : FEATURES: waiting...
D, [2013-02-18T03:24:55.784785 #15238] DEBUG -- : FEATURES: received
D, [2013-02-18T03:24:55.785290 #15238] DEBUG -- : PROCESSING:
<stream:features xmlns='jabber:client'><mechanisms 
xmlns='urn:ietf:params:xml:ns:xmpp-sasl'><mechanism>SCRAM-SHA-1</mechanism>    
<mechanism>DIGEST-MD5</mechanism><mechanism>PLAIN</mechanism></mechanisms><c 
hash='sha-1' node='http://www.process-one.net/en/ejabberd/' 
ver='yy7di5kE0syuCXOQTXNBTclpNTo=' xmlns='http://jabber.org/protocol/caps'/>
<register xmlns='http://jabber.org/features/iq-register'/></stream:features> 
(REXML::Element)
D, [2013-02-18T03:24:55.785360 #15238] DEBUG -- : FEATURES: waiting finished
D, [2013-02-18T03:24:55.785450 #15238] DEBUG -- : TRYING stanzacbs...
D, [2013-02-18T03:24:55.785556 #15238] DEBUG -- : TRYING 
message/iq/presence/cbs...    
D, [2013-02-18T03:24:55.785987 #15238] DEBUG -- : SENDING:
<auth mechanism='DIGEST-MD5' xmlns='urn:ietf:params:xml:ns:xmpp-sasl'/>
D, [2013-02-18T03:24:55.787784 #15238] DEBUG -- : RECEIVED:
<challenge xmlns='urn:ietf:params:xml:ns:xmppsasl'>bm9uY2U9IjE5NzEzNzk5NzQiLHFvcD0iYXV0aCIsY2hhcnNldD11dGYtOCxhbGdvcml0aG09bWQ1LXNlc3M=</challenge>
D, [2013-02-18T03:24:55.788143 #15238] DEBUG -- : SASL DIGEST-MD5 challenge:
nonce="1971379974",qop="auth",charset=utf-8,algorithm=md5-sess
{"nonce"=>"1971379974", "qop"=>"auth", "charset"=>"utf-8", "algorithm"=>"md5-sess"}
D, [2013-02-18T03:24:55.788475 #15238] DEBUG -- : SASL DIGEST-MD5 response:
nonce="1971379974",charset=utf-8,username="chat",realm="localhost",cnonce="92a37c018b9c32339c4b52ee0b02d67e",nc=00000001,qop=auth,digest-uri="xmpp/localhost",response=9c6bca9f4cf6f8daf3197a914023729a
{"nonce"=>"\"1971379974\"", "charset"=>"utf-8", "username"=>"\"chat\"", "realm"=>"\"localhost\"", "cnonce"=>"\"92a37c018b9c32339c4b52ee0b02d67e\"", "nc"=>"00000001", "qop"=>"auth", "digest-uri"=>"\"xmpp/localhost\"", "response"=>"9c6bca9f4cf6f8daf3197a914023729a"}
D, [2013-02-18T03:24:55.788761 #15238] DEBUG -- : SENDING:
<response xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>bm9uY2U9IjE5NzEzNzk5NzQiLGNoYXJzZXQ9dXRmLTgsdXNlcm5hbWU9ImNoYXQiLHJlYWxtPSJsb2NhbGhvc3QiLGNub25jZT0iOTJhMzdjMDE4YjljMzIzMzljNGI1MmVlMGIwMmQ2N2UiLG5jPTAwMDAwMD
   AxLHFvcD1hdXRoLGRpZ2VzdC11cmk9InhtcHAvbG9jYWxob3N0IixyZXNwb25zZT05YzZiY2E5ZjRjZjZm
OGRhZjMxOTdhOTE0MDIzNzI5YQ==</response>
D, [2013-02-18T03:24:55.818548 #15238] DEBUG -- : RECEIVED:
<challenge xmlns='urn:ietf:params:xml:ns:xmpp-    
sasl'>cnNwYXV0aD01NTQ2NTVlYjljYWUzZGExMjhmZjBkMWUzNDUzYmIwZQ==</challenge>
D, [2013-02-18T03:24:55.819064 #15238] DEBUG -- : SENDING:
<response xmlns='urn:ietf:params:xml:ns:xmpp-sasl'/>
D, [2013-02-18T03:24:56.147820 #15238] DEBUG -- : RECEIVED:
<success xmlns='urn:ietf:params:xml:ns:xmpp-sasl'/>
D, [2013-02-18T03:24:56.148432 #15238] DEBUG -- : SENDING:
<stream:stream xmlns:stream='http://etherx.jabber.org/streams' 
xmlns='jabber:client' to='localhost' xml:lang='en' version='1.0' >
D, [2013-02-18T03:25:56.151018 #15238] DEBUG -- : SENDING:

W, [2013-02-18T03:25:56.212617 #15238]  WARN -- : EXCEPTION:
Jabber::ServerDisconnected
Server Disconnected!
/home/ubuntu/.rvm/gems/ruby-1.9.3-p385/gems/xmpp4r-
0.5/lib/xmpp4r/streamparser.rb:68:in `block in parse'
/home/ubuntu/.rvm/rubies/ruby-1.9.3-
p385/lib/ruby/1.9.1/rexml/parsers/sax2parser.rb:195:in `call'
/home/ubuntu/.rvm/rubies/ruby-1.9.3-
p385/lib/ruby/1.9.1/rexml/parsers/sax2parser.rb:195:in `block in handle'
/home/ubuntu/.rvm/rubies/ruby-1.9.3-
p385/lib/ruby/1.9.1/rexml/parsers/sax2parser.rb:195:in `each'
/home/ubuntu/.rvm/rubies/ruby-1.9.3-
p385/lib/ruby/1.9.1/rexml/parsers/sax2parser.rb:195:in `handle'
/home/ubuntu/.rvm/rubies/ruby-1.9.3-
p385/lib/ruby/1.9.1/rexml/parsers/sax2parser.rb:95:in `parse'
/home/ubuntu/.rvm/gems/ruby-1.9.3-p385/gems/xmpp4r-    
0.5/lib/xmpp4r/streamparser.rb:79:in `parse'
/home/ubuntu/.rvm/gems/ruby-1.9.3-p385/gems/xmpp4r-
0.5/lib/xmpp4r/stream.rb:75:in `block in start'
W, [2013-02-18T03:25:56.212740 #15238]  WARN -- : Exception caught in Parser 
thread! (Jabber::ServerDisconnected)
/home/ubuntu/.rvm/gems/ruby-1.9.3-p385/gems/xmpp4r-    
0.5/lib/xmpp4r/streamparser.rb:68:in `block in parse'

有任何想法吗?

谢谢!!

4

2 回答 2

0

如文档中所述,有一种更好的方法来处理错误:

If you want your connection to survive disconnects and timeouts, catch exception in Stream#on_exception and re-call Client#connect and Client#auth. Don‘t forget to re-send initial Presence and everything else you need to setup your session.

Stream#on_exception 链接

于 2014-09-19T12:39:32.457 回答
0

使用 Jabber 通过 facebook 发送消息时,我也遇到了这个问题。

即使包裹在 try/catch 中,有时也会Jabber::ServerDisconnected抛出并导致 Web 服务器停止。在 xmpp4r 源代码中,它们Jabber::ServerDisconnected在收到断开连接的信号时和Thread.abort_on_exception = true在每个线程中抛出

参考https://github.com/lnussbaum/xmpp4r/blob/master/lib/xmpp4r/streamparser.rbhttps://github.com/lnussbaum/xmpp4r/blob/master/lib/xmpp4r/stream.rb

我猜在孤立线程中抛出了异常。我没有解决方案,但有一个Thread::abort_on_exception = false全局设置的解决方法(在使用 Jabber 的类/方法中)。所以仍然抛出异常,但我们忽略它。

于 2014-03-24T15:54:21.300 回答