我目前正在尝试使用可用的RTM API创建带有Scala的#slack bot。
我已经设法获得一个基本的功能机器人,响应一个" ping"用" pong"。
我目前遇到的问题是在打开流后每隔几分钟就会系统地关闭websocket连接。
我目前正在为websockets使用以下库:https://github.com/jfarcand/WCS
此时,我不完全确定在从#slack RTM流接收的空JSON消息({}
)之后导致连接丢失的原因。
非常感谢任何帮助。
这里是相关的连接和监听器代码:
def connect(): Unit ={
if(rec_url == "")
slack = ws.open(rtm_url)
else {
h.debug("Attempting to reconnect")
slack = WebSocket().open(rec_url) // TODO: need to completely close the connection before trying to create a new one.
}
initRTM()
}
// Initialize Real Time Messaging
def initRTM(): Unit = {
h.debug("\nOpening Real Time Messaging socket")
slack.listener(new TextListener {
override def onOpen: Unit = {
h.debug("Websocket connection open")
status = 1
}
override def onClose: Unit = {
h.debug("Websocket connection closed, reconnecting...")
slack.shutDown
status = 0
}
override def onError(t: Throwable): Unit ={
h.debug("Websocket Error encountered")
t.printStackTrace()
h.debug(t.getMessage)
}
override def onMessage(message: String) {
h.debug("Message received: "+message)
val body: JsValue = Json.parse(message)
try {
if(body.as[JsObject].keys.contains("type")){
val m_type = (body \ "type").as[String]
m_type match {
case "hello" => ;
case "reconnect_url" => setReconnectUrl((body \ "url").as[String])
case "message" => processMessage(body)
case _ => h.debug("Unprocessed message type: " + m_type)
}
}
} catch {
case e: Exception=> e.printStackTrace()
}
}
})
while(slack.isOpen){
}
h.debug("connection is closed")
connect()
}
以下是控制台的输出:
Opening Real Time Messaging socket
INFO - Websocket connection open
INFO - Message received: {"type":"hello"}
INFO - Message received: {"reply_to":0,"type":"message","channel":"D1NCSAU12","user":"U1NCSAU0L","text":"pong","ts":"1467558776.000079"}
INFO - Message received: {"type":"reconnect_url","url":"wss://mpmulti-c6k1.slack-msgs.com/websocket/x4UkvMwZeNqFuyZ_YsxjLNi_OIOzqHisL6sHC3DB0QRnKoG-VH8Qr361SlSprlWb6WjzDhw6j5Pj0FiFFYTjoiCLwM-i863os0xWkjUGJJbUoKUmtlG22e3lTTAFuuIFg2TTI7W-0XfU4HJB2nvbjy_hKCwVQ7uIIlrr6fYi_ms="}
INFO - Reconnect URL Set to: wss://mpmulti-c6k1.slack-msgs.com/websocket/x4UkvMwZeNqFuyZ_YsxjLNi_OIOzqHisL6sHC3DB0QRnKoG-VH8Qr361SlSprlWb6WjzDhw6j5Pj0FiFFYTjoiCLwM-i863os0xWkjUGJJbUoKUmtlG22e3lTTAFuuIFg2TTI7W-0XfU4HJB2nvbjy_hKCwVQ7uIIlrr6fYi_ms=
INFO - Message received: {"type":"presence_change","presence":"active","user":"U1NCSAU0L"}
INFO - Unprocessed message type: presence_change
11:23:18.790 [Hashed wheel timer #1] DEBUG com.ning.http.client.providers.netty.channel.pool.DefaultChannelPool - Closed 0 connections out of 0 in 0ms
INFO - Message received: {"type":"user_typing","channel":"D1NCSAU12","user":"U1MP4A19R"}
INFO - Unprocessed message type: user_typing
INFO - Message received: {"type":"message","channel":"D1NCSAU12","user":"U1MP4A19R","text":"ping","ts":"1467559430.000080","team":"T1MQWNFR8"}
INFO - User typed 'ping'
INFO - Answer: {"id":0,"type":"message","channel":"D1NCSAU12","text":"pong"}
INFO - Message received: {"ok":true,"reply_to":0,"ts":"1467559430.000081","text":"pong"}
INFO - Message received: {"type":"user_typing","channel":"D1NCSAU12","user":"U1MP4A19R"}
INFO - Unprocessed message type: user_typing
INFO - Message received: {"type":"message","channel":"D1NCSAU12","user":"U1MP4A19R","text":"ping","ts":"1467559432.000082","team":"T1MQWNFR8"}
INFO - User typed 'ping'
INFO - Answer: {"id":1,"type":"message","channel":"D1NCSAU12","text":"pong"}
INFO - Message received: {"ok":true,"reply_to":1,"ts":"1467559432.000083","text":"pong"}
INFO - Message received: {"type":"user_typing","channel":"D1NCSAU12","user":"U1MP4A19R"}
INFO - Unprocessed message type: user_typing
INFO - Message received: {"type":"message","channel":"D1NCSAU12","user":"U1MP4A19R","text":"ping","ts":"1467559434.000084","team":"T1MQWNFR8"}
INFO - User typed 'ping'
INFO - Answer: {"id":2,"type":"message","channel":"D1NCSAU12","text":"pong"}
INFO - Message received: {"ok":true,"reply_to":2,"ts":"1467559434.000085","text":"pong"}
INFO - Message received: {"type":"user_typing","channel":"D1NCSAU12","user":"U1MP4A19R"}
INFO - Unprocessed message type: user_typing
INFO - Message received: {"type":"message","channel":"D1NCSAU12","user":"U1MP4A19R","text":"ping","ts":"1467559448.000086","team":"T1MQWNFR8"}
INFO - User typed 'ping'
INFO - Answer: {"id":3,"type":"message","channel":"D1NCSAU12","text":"pong"}
INFO - Message received: {"ok":true,"reply_to":3,"ts":"1467559448.000087","text":"pong"}
INFO - Message received: {"type":"user_typing","channel":"D1NCSAU12","user":"U1MP4A19R"}
INFO - Unprocessed message type: user_typing
INFO - Message received: {"type":"message","channel":"D1NCSAU12","user":"U1MP4A19R","text":"ping ping","ts":"1467559452.000088","team":"T1MQWNFR8"}
11:24:18.889 [Hashed wheel timer #1] DEBUG com.ning.http.client.providers.netty.channel.pool.DefaultChannelPool - Closed 0 connections out of 0 in 0ms
INFO - Message received: {"type":"reconnect_url","url":"wss://mpmulti-lbww.slack-msgs.com/websocket/u61Fem7nt3c1DP35C1So6S-Q3QnP0wcY4BeKMG6GZBpjo32E_rGM0YhwH-M_i6uGdezSgzr8R6BmM4eC7ZcwGaAR38GRi2VFyEM7REgtCO0Hd6FAsguHS63TwCI65UwBCkcS_gEFdpoI5tD0az4cBWtdfj1yXbn1iOwpiH_BALg="}
INFO - Reconnect URL Set to: wss://mpmulti-lbww.slack-msgs.com/websocket/u61Fem7nt3c1DP35C1So6S-Q3QnP0wcY4BeKMG6GZBpjo32E_rGM0YhwH-M_i6uGdezSgzr8R6BmM4eC7ZcwGaAR38GRi2VFyEM7REgtCO0Hd6FAsguHS63TwCI65UwBCkcS_gEFdpoI5tD0az4cBWtdfj1yXbn1iOwpiH_BALg=
11:25:18.989 [Hashed wheel timer #1] DEBUG com.ning.http.client.providers.netty.channel.pool.DefaultChannelPool - Closed 0 connections out of 0 in 0ms
INFO - Message received: {}
11:26:13.918 [New I/O worker #1] DEBUG com.ning.http.client.providers.netty.handler.Processor - Channel Closed: [id: 0x0e0d06c0, /192.168.99.102:46345 :> mpmulti-6wbl.slack-msgs.com/54.172.207.190:443] with attribute NettyResponseFuture{currentRetry=5,
isDone=true,
isCancelled=false,
asyncHandler=com.ning.http.client.ws.WebSocketUpgradeHandler@e9cba57,
nettyRequest=com.ning.http.client.providers.netty.request.NettyRequest@b3f8453,
content=NettyWebSocket{channel=[id: 0x0e0d06c0, /192.168.99.102:46345 :> mpmulti-6wbl.slack-msgs.com/54.172.207.190:443]},
uri=wss://mpmulti-6wbl.slack-msgs.com/websocket/0WNxIQsK_mzw561vkxWrN4B3tSPO-oBBR6fPtGnD2GLP_47Cms8s8GzyNl8ujheXVnNIw0RygTwglxlZYdfChlNf_0MfCwihOeQMUI-hjgCcwxXuMZFUSYrZphQpu1w7VYP5j3dc0nOVnL0YZX_oi62cgeoaWgwn5GjUiif-0AM=,
keepAlive=true,
httpHeaders=org.jboss.netty.handler.codec.http.DefaultHttpHeaders@790add3d,
exEx=null,
redirectCount=0,
timeoutsHolder=null,
inAuth=false,
statusReceived=false,
touch=530188604}
INFO - Websocket connection closed, reconnecting...
11:26:13.920 [New I/O worker #1] DEBUG com.ning.http.client.providers.netty.channel.ChannelManager - Closing Channel [id: 0x0e0d06c0, /192.168.99.102:46345 :> mpmulti-6wbl.slack-msgs.com/54.172.207.190:443]
答案 0 :(得分:4)
在对这个问题进行了一些额外的研究并尝试了各种方法后,我尽力解决了这个问题。
似乎#slack API需要定期发送ping消息,以防止连接被丢弃。
在实施定时器代码以每30秒发送一次ping消息之后,到目前为止我还没有遇到过这个问题。
这是我为解决问题而添加的部分代码:
def initRTM(): Unit = {
h.debug("\nOpening Real Time Messaging socket")
h.debug("Pinging at 30s intervals")
val ex = new ScheduledThreadPoolExecutor(1)
val task = new Runnable {
def run() = sendPing()
}
val f = ex.scheduleAtFixedRate(task, 1, 30, TimeUnit.SECONDS)
if(!slack.isOpen){
f.cancel(false)
}
[...]
}
RTM API(https://api.slack.com/rtm)没有明确规定发送ping消息是强制保持连接活动的必要条件,但强烈建议:
即使在空闲时,客户也应该尝试快速检测断线 句点,使用户可以轻松分辨出存在的差异 断开了,每个人都很安静。并非所有Web浏览器都支持 WebSocket ping规范,因此RTM协议也支持ping / pong 消息。当没有其他活动时,客户端应该发送ping 每隔几秒
这实际上似乎是为了维持连接的必要条件。
我希望在接近#slack RTM API时,这有助于其他用户遇到类似问题。