I would like to use WebSockets in Scala and Play Framework. But I can’t get the Echo-server example to work.
What should I import for await() and disconnect()?
The error I get is Error raised is : not found: value await. I used the code below:
package controllers
import play._
import play.mvc._
import play.mvc.Http.WebSocketEvent
import play.mvc.Http.WebSocketFrame
import play.mvc.Http.WebSocketClose
import play.mvc.WebSocketController
object MySocket extends WebSocketController {
def echo = {
while(Http.Inbound.current().isOpen()) {
val e : WebSocketEvent =
await(Http.Inbound.current().nextEvent()).asInstanceOf[WebSocketEvent]
if(e.isInstanceOf[WebSocketFrame]) {
val frame : WebSocketFrame = e.asInstanceOf[WebSocketFrame]
if(!frame.isBinary) {
if(frame.textData.equals("quit")) {
Http.Outbound.current().send("Bye!");
disconnect();
} else {
Http.Outbound.current().send("Echo: " + frame.textData)
}
}
}
if(e.isInstanceOf[WebSocketClose]) {
Logger.info("Socket closed!")
}
}
}
}
Here is the compilation error in the Terminal:
Compiling:
/Users/jonas/play-1.2.2RC1/jonassite/app/MySocket.scala
/Users/jonas/play-1.2.2RC1/jonassite/app/MySocket.scala:14: not found: value await
val e : WebSocketEvent = await(Http.Inbound.current().nextEvent()).asInstanceOf[WebSocketEvent]
^
/Users/jonas/play-1.2.2RC1/jonassite/app/MySocket.scala:20: not found: value disconnect
disconnect();
^
two errors found
Compiling:
/Users/jonas/play-1.2.2RC1/jonassite/app/MySocket.scala
/Users/jonas/play-1.2.2RC1/jonassite/app/MySocket.scala:14: not found: value await
val e : WebSocketEvent = await(Http.Inbound.current().nextEvent()).asInstanceOf[WebSocketEvent]
^
/Users/jonas/play-1.2.2RC1/jonassite/app/MySocket.scala:20: not found: value disconnect
disconnect();
^
two errors found
12:52:57,049 ERROR ~
@66lce6kp8
Internal Server Error (500) for request GET /handshake
Compilation error (In /app/MySocket.scala around line 14)
The file /app/MySocket.scala could not be compiled. Error raised is : not found: value await
play.exceptions.CompilationException: not found: value await
at play.scalasupport.ScalaPlugin.compilationException(ScalaPlugin.scala:129)
at play.scalasupport.ScalaPlugin.detectClassesChange(ScalaPlugin.scala:115)
at play.plugins.PluginCollection.detectClassesChange(PluginCollection.java:358)
at play.Play.detectChanges(Play.java:591)
at play.Invoker$Invocation.init(Invoker.java:186)
at Invocation.HTTP Request(Play!)
await()anddisconnect()are methods available from theWebSocketController. However, these are currently only available in the Java version, and not Scala. See this post here on the play groups for more information.This should be available in the 1.0 release of the scala plugin, but for now if you want to use the aysnc features (await etc), then you will have to use Java, or take a look at the Java wrapper that one of the Play users have developed.