我正在尝试使用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");
答案 0 :(得分:0)
您似乎正在尝试将WebSocket连接与标准http路由连接,该路由以状态代码200回答您。