WebSocket using Iteratee
Let's define a WebSocket connection, which accepts strings and sends back the reverse of a string using Iteratee:
def websocketBroadcast = WebSocket.using[String] { request => val (out, channel) = Concurrent.broadcast[String] val in = Iteratee.foreach[String] { word => channel.push(word.reverse) } (in, out) }
The WebSocket.using
method creates a WebSocket of a specific type using an Iteratee (inbound channel) and its corresponding enumerator (outbound channel). In the preceding code snippet, we return a tuple of the Iteratee in and the Enumerator out.
The Concurrent
object is also a helper, which provides utilities to use Iteratees, Enumerators, and Enumeratees concurrently. The broadcast[E]
method creates an Enumerator and a channel and returns a (Enumerator[E], Channel[E])
tuple. The Enumerator and channel, thus obtained, can be used to broadcast data to multiple Iteratees.
After this, we need to bind it to a path in the...