mashupbots / socko

A Scala web server powered by Netty networking and AKKA processing.
Other
255 stars 51 forks source link

Need a safe way to resolve the webserver.webSocketConnections when defining Routes #76

Closed simbo1905 closed 10 years ago

simbo1905 commented 10 years ago

In this file

https://github.com/simbo1905/planning-poker/blob/v0.6/src/main/scala/scrumpoker/server/ScrumPokerApp.scala

at lines lines 99 and 117 the response from the business logic actors is pushed to the websockets. This requires me to use a "var" for the webServer as using a "val" gives the compile error "forward reference extends over definition of value routes". The issues being that you have to define the routes to use them in the construction of the webserver then you want to use a reference to the webserver in the routes.

Can the api be enhanced so that there is a way in the routes to lookup the webserver to interact with the webserver.webSocketConnections from within the routes?

veebs commented 10 years ago

Try what I've done in the ChatApp example:

https://github.com/mashupbots/socko/blob/master/socko-examples/src/main/scala/org/mashupbots/socko/examples/websocket/ChatApp.scala

An alternative is to start up a wrapper actor in main() with webServer as an argument. You can then send messages to the actor which will use webServer to send messages.

class MyActor(webServer: WebServer) extends Actor {
  val log = Logging(context.system, this)
  def receive = {
    case s: String ⇒ webServer.webSocketConnections.writeText(s)
    case _      ⇒ log.info("received unknown message")
  }
}
simbo1905 commented 10 years ago

I have updated the code accordingly at: https://github.com/simbo1905/planning-poker/blob/v0.7/src/main/scala/scrumpoker/server/ScrumPokerApp.scala https://github.com/simbo1905/planning-poker/blob/v0.7/src/main/scala/scrumpoker/game/ScrumEngine.scala As per your suggestion in the main method the webserver is passed into the actorFor factory to create the game actor. Then in the routes actorSelector is used to lookup a reference to the game actor.

It is the use of an actor lookup in the routes which breaks the "forward reference" issue which I was not groking. Thanks for the advice.