hangscer

playframework-Websocket环境搭建

2017/05/01

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package controllers
import akka.actor.{Actor, ActorRef, ActorSystem, Props}
import akka.stream.Materializer
import com.google.inject.Inject
import play.api.libs.streams.ActorFlow
import play.api.mvc._
class WebSocketCustomerController @Inject()( implicit system:ActorSystem ,materializer:Materializer) {
def socket=WebSocket.accept[String,String]( request=>
ActorFlow.actorRef(out => MyWebSocketActor.props(out))
)
}
object MyWebSocketActor{
def props(out:ActorRef)=Props(classOf[MyWebSocketActor],out)
}
class MyWebSocketActor(out:ActorRef) extends Actor{
override def receive: Receive = {
case msg:String =>
out!("I received your message :"+msg)
}
}

这段代码是抄官网上的,但是并没有给出具体配置,而且IDEA插件对play支持仍然有欠缺.
route配置为:

1
2
GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset)
GET /ws controllers.WebSocketCustomerController.socket #IDEA可能无法识别这句话

Websocket路径为ws://localhost:9000/ws.