简体   繁体   English

在播放框架中关闭Twitter流连接

[英]Close twitter streaming connection in play framework

I connect to twitter streaming API using Play 2.2 WS API in the code example below. 我在下面的代码示例中使用Play 2.2 WS API连接到twitter流API。 I'm stuck trying to figure out how to disconnect from stream once it's established. 我一直试图找出建立流后如何断开流的方法。 Is there any proper way to do that rather than stopping application? 有什么适当的方法可以做到这一点,而不是停止应用程序? Any help will be appreciated. 任何帮助将不胜感激。

def watchTweets(keywords : String) = Action { implicit request =>
Logger.debug(s"watchTweets invoked with: $keywords")
val (tweetsOut, tweetChanel) = Concurrent.broadcast[JsValue]
WS.url(s"https://stream.twitter.com/1.1/statuses/filter.json?track=" + URLEncoder.encode(keywords, "UTF-8"))
  .sign(OAuthCalculator(Twitter.KEY, Twitter.sessionTokenPair.get))
  .postAndRetrieveStream("")(headers => Iteratee.foreach[Array[Byte]] { ba =>
  val msg = new String(ba, "UTF-8")
  Logger.debug(s"received message: $msg")
  val tweet = Json.parse(msg)
  tweetChanel.push(tweet)
}).flatMap(_.run)

Ok.chunked(tweetsOut &> Comet(callback = "parent.cometMessage")) }

This is a known issue... the answer is that you close the connection, but the problem is, the enumerator won't notice that the connection is closed until it tries to feed a tweet to the client, and that won't happen until it receives another tweet from Twitter, which might take a long time to happen. 这是一个已知的问题...答案是您关闭了连接,但是问题是,枚举器在尝试将tweet馈送到客户端之前不会注意到该连接已关闭,并且这种情况不会发生直到它收到来自Twitter的另一条推文,这可能要花很长时间。

This is of course a problem when doing Twitter streaming because Twitter will only let a user create one stream at a time, so that effectively means that you can't do a second stream until the first receives some data. 在进行Twitter流式传输时,这当然是一个问题,因为Twitter一次只允许用户创建一个流,因此有效地意味着您不能在第二个流传输之前,先接收一个数据。

Unfortunately we don't have a work around as yet, but we are looking at introducing a new lower level streaming API into Play around the 2.4 time line, which will certainly allow this. 不幸的是,我们还没有解决的办法,但是我们正在考虑在2.4时间轴上向Play引入新的较低级别的流API,这肯定会允许这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM