Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Play Framework as a component

I'm trying to add a Play Framework web server as part of a larger application, but the application's primary purpose is NOT a web server. It already accepts a variety of connections, including serial and TCP, but I need to add WebSockets, and Play Framework's WebSocket interface is preferable. (We've tried Undertow, and encountered too many problems with its clunky interface)

I've only gotten as far as creating an application, and starting it, but I'm unable to connect to it. After I run the following code, nothing is listening on port 8000. What do I need to do?

application.conf:

play.server.http.port=8000
http.port=8000

webserver.scala:

def startWebServer = {
  val environment = new Environment(
    new File("/path/to/app"),
    classOf[Dummy].getClassLoader,
    play.api.Mode.Dev
  )
  val context = play.api.ApplicationLoader.createContext(environment)
  val application = ApplicationLoader(context).load(context)

  play.api.Play.start(application)
}

build.sbt:

libraryDependencies += "com.typesafe.play" %% "play" % "2.5.0-M1"

Output:

[info] play.api.Play - Application started (Dev)

You can download the code here: github.com/alancnet/playtest

like image 457
wizulus Avatar asked Oct 18 '22 23:10

wizulus


1 Answers

That was just the application. It still needs a host. Add the following code:

webserver.scala:

  play.core.server.NettyServer.fromApplication(
    application
  )

build.sbt:

libraryDependencies += "com.typesafe.play" %% "play-netty-server" % "2.5.0-M1"

output:

[info] play.api.Play - Application started (Dev)
[info] p.c.s.NettyServer - Listening for HTTP on /0:0:0:0:0:0:0:0:9000

EDIT: Here's the code that went into production: https://gist.github.com/alancnet/68f6e787e1ab96bd1c4a

like image 60
wizulus Avatar answered Oct 22 '22 22:10

wizulus