播放ScalaWebSocket v2.5。*需要升级到WebSocket

时间:2016-06-21 03:26:54

标签: scala playframework websocket

我尝试使用Play ScalaWebsockets创建一个websocket。我提到了位于here的文档。目前,我正在使用这些SBT插件,这些插件需要使用Play的网页插座。

  "com.typesafe.akka" %% "akka-actor" % "2.4.7",
  "com.typesafe.akka" %% "akka-testkit" % "2.4.7" % "test",

这是我的演员:

 package actors.chat

 import akka.actor._
 import akka.stream._
 object ChatSocketActor {
    def props(out: ActorRef) = Props(new ChatSocketActor(out))
 }

 class ChatSocketActor(out: ActorRef) extends Actor {
    def receive = {
      case msg: String =>
         out ! ("I received your message: " + msg)
   }
 }

在我的控制器中,我有这个套接字功能:

 @Singleton
 class ChatController @Inject()(
    val messagesApi: MessagesApi,
    val pageMetaApi: PageMetaApi,
    implicit val materializer: Materializer,
    implicit val system: ActorSystem,
    implicit val wja: WebJarAssets
 ) extends Controller with I18nSupport with PageMetaSupport {

    import actors.chat.ChatSocketActor

    def index = Action { implicit request =>
       Ok(views.html.chat.index());
    }

    def socket = WebSocket.accept[String, String] { request =>
       ActorFlow.actorRef(out => ChatSocketActor.props(out))
    }

 }

编译并刷新浏览器后,我得到了426响应。

 Upgrade to WebSocket required

我错过了什么吗?

问题已解决:

我是websockets的新手。问题是我在标准HTTP上访问套接字。我将路线改为

# Chat
GET   /chat                       controllers.ChatController.index
GET   /websocket                  controllers.ChatController.socket

在/ chat页面中调用websocket连接的JS脚本。

var ws = new WebSocket("ws://127.0.0.1:9000/websocket");
console.log(ws)

0 个答案:

没有答案