我尝试使用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'
有什么想法吗?
谢谢!
答案 0 :(得分: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.rb和https://github.com/lnussbaum/xmpp4r/blob/master/lib/xmpp4r/stream.rb
我猜在孤儿线程中抛出异常。
我没有解决方案,但是全局设置Thread::abort_on_exception = false
的解决方法(在使用Jabber的类/方法中)。所以异常仍然抛出,但我们忽略它。
答案 1 :(得分:0)
有一种更好的处理错误的方法,如docs中所述:
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.