2016-07-03 5 views
2

現在RTM APIを使用してScalaで#slackボットを作成しようとしています。#slack「{}」メッセージの後に続くRTM APIの切断

"ping"と "pong"に応答する基本的な機能的なボットを手に入れました。

私が現在経験している問題は、ストリームの開始から数分ごとにwebsocket接続が体系的に閉じられることです。私は現在、WebSocketをのための次のライブラリ使用してい

:この時点でhttps://github.com/jfarcand/WCS

を、私は#slackから受信された空のJSONメッセージ({})次の接続のドロップを引き起こしているものを全くわかりませんRTMストリーム。

ご協力いただければ幸いです。ここで

は、関連する接続とリスナーのコードです:ここで

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, 
    [email protected]ba57, 
    nettyRe[email protected]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, 
    ht[email protected]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] 

答えて

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に近づいても同様の問題を抱えている他のユーザーに役立ちたいと考えています。

+0

どのようにpingを送信しましたか? – dopatraman

+0

「Ping and Pong」セクションのRTMドキュメント(https://api.slack.com/rtm)を参照してください。このメッセージは定期的に送信する必要があります。このメッセージは他のメッセージと同様に送信されます。 – autronix

+0

が優れています。なぜ私のクライアントが無作為に動作しなくなったのか理解しようとしていました。 +1。 – dopatraman

関連する問題