4

I'm a beginner with Finagle.

Today I tried to put together a hello world with finagle-websocket, but I can't understand why, when I run it, it doesn't block listening and dies. TestWS.scala:

package foo.bar

import com.twitter.finagle.HttpWebSocket
import com.twitter.finagle.Service
import com.twitter.finagle.websocket.WebSocket
import com.twitter.util.Future
import com.twitter.concurrent.Broker
import com.twitter.util.RandomSocket

object TestWS {
  def main(args: Array[String]) {
    val server = HttpWebSocket.serve(RandomSocket(), new Service[WebSocket, WebSocket] {
      def apply(req: WebSocket): Future[WebSocket] = {
        val outgoing = new Broker[String]
        val socket = req.copy(messages = outgoing.recv)
        req.messages foreach { outgoing ! _.reverse }
        Future.value(socket)
      }
    })
  }
}

outputs only these lines before dying:

May 19, 2014 4:00:35 PM com.twitter.finagle.Init$ apply
INFO: Finagle version 6.14.0 (rev=3c3e9b0370f67cb739feca51469dc20eb35aab67) built at 20140417-

What am I doing wrong?

sscarduzio
  • 5,938
  • 5
  • 42
  • 54

2 Answers2

4

The idiomatic way to wait for the server is:

val server = ...
Await.ready(server)
Steve Gury
  • 15,158
  • 6
  • 38
  • 42
1

The program is exiting before any connections are made. Try adding

new CountDownLatch(1).await();

(or similar) after HttpWebSocket.serve() ?

tariksbl
  • 1,069
  • 6
  • 20
  • It worked. In other Finagle example servers this was not needed, weird. Finagle-websocket README.md needs to be fixed. I will report this via GH issues. – sscarduzio May 20 '14 at 08:06