dcaoyuan / spray-socketio

socket.io for spray
Apache License 2.0
253 stars 55 forks source link

Integration with spray-routing #69

Closed labe-me closed 9 years ago

labe-me commented 9 years ago

Hello,

I am a beginner with akka and spray and would appreciate an exemple of socketio + spray-routing integration.

Is it possible to integrate spray-socketio inside a route or does it have to be the only running server actor ?

If it has to be the main server I imagine that the HttpRequest could be redirected to an HttpService or a inside the def genericLogic: Receive = { case x:HttpRequest => ... } but I am not sure how to do it.

That's where a code sample could be really helpful for newbies like me :)

Thank for the help and for the code Laurent

labe-me commented 9 years ago

I got something working but I am not sure it's the most efficient and secure way of doing the integration

First build the Route:

import spray.routing._
import spray.routing.Directives._

  var route : Route = 
    path("test") {
      get {
        complete { "That's it baby" }
      }
    }

Then run it from the SocketIOWorker.genericLogic method:

      case x: HttpRequest =>
        log.info("Got http req uri = {}", x.uri.path.toString.split("/").toList)
        route(RequestContext(x, sender, x.uri.path))

It works but doesn't handle exceptions like in: https://github.com/spray/spray/blob/master/spray-routing/src/main/scala/spray/routing/HttpService.scala

Is it the right approach?