5

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

wizulus
  • 3,810
  • 2
  • 19
  • 36
  • Is there any output in the console when you run `webserver.scala`? – Gavin Schulz Oct 24 '15 at 08:05
  • Maybe this is already a solution: http://stackoverflow.com/questions/8205067/how-do-i-change-the-default-port-9000-that-play-uses-when-i-execute-the-run?answertab=votes#tab-top – Kris Oct 24 '15 at 12:55
  • @GavinSchulz I added the output above. – wizulus Oct 24 '15 at 23:01
  • @Kris Thanks, but it is not a solution. Those instructions run the application through activator. I am not running an activator application. – wizulus Oct 24 '15 at 23:02
  • @alancnet have you encountered any special oddities in using play like this, meaning, as a library? – matanster Mar 06 '16 at 22:04
  • Not many.. Probably just routing and serving local files. Here's the code that ended up going to production: https://gist.github.com/alancnet/68f6e787e1ab96bd1c4a Of course, you'll have to extend LocalWebServer, and implement routes, but its pretty straight forward after you have the basics. – wizulus Mar 07 '16 at 01:57

1 Answers1

3

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

wizulus
  • 3,810
  • 2
  • 19
  • 36