[英]Play 2.4 WebSocket Actor doesn't respond
我正在嘗試將PlayFramework中的WebSocket與Akka Actor一起使用,但是當我嘗試使用chrome或firefox調用它時,它不起作用:
var exampleSocket = new WebSocket("ws://127.0.0.1:9000");
WebSocket connection to 'ws://127.0.0.1:9000/' failed: Error during WebSocket handshake: Unexpected response code: 200
我的控制器摘錄:
package controllers
import javax.inject.Inject
import play.api.libs.ws.WSClient
import play.api.mvc.Controller
import akka.actor.ActorSystem
import actors.ContainersActor
import play.api.mvc.WebSocket
import play.api.Play.current
import play.api.libs.concurrent.Execution.Implicits.defaultContext
class ContainersController @Inject() (ws: WSClient, system: ActorSystem) extends Controller {
def socket = WebSocket.acceptWithActor[String, String] { request => out =>
ContainersActor.props(out)
}
println("socket : "+socket);
}
我的演員:
package actors
import akka.actor.Actor
import akka.actor.ActorRef
import akka.actor.Props
import akka.actor.actorRef2Scala
object ContainersActor {
def props(out: ActorRef) = Props(new ContainersActor(out))
}
class ContainersActor(out: ActorRef) extends Actor {
def receive = {
case msg: String =>
out ! ("I received your message: " + msg)
}
}
我遵循以下文檔: https : //www.playframework.com/documentation/2.4.x/ScalaWebSockets
謝謝您的幫助 :)
解決方案:我沒有為套接字建立路由,我認為這是另一個協議x)
我在conf / routes上添加了這一行
GET /socket controllers.ContainersController.socket
我用以下命令調用套接字:
var exampleSocket = new WebSocket("ws://127.0.0.1:9000/socket");
似乎您正在嘗試將WebSocket連接連接到標准的http路由,從而用狀態碼200回答您。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.