简体   繁体   中英

Play 2.4 WebSocket Actor doesn't respond

I'm trying to use the WebSocket from PlayFramework with an Akka Actor but when I try to call it with chrome or firefox it doesn't work :

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

Extract of my Controller :

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);
}

My Actor :

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)
  }
}

I follow this documentation : https://www.playframework.com/documentation/2.4.x/ScalaWebSockets

Thank you for your help :)

Solution : I didn't make a route for the socket I thought it was an other protocol x)

I added this line on my conf/routes

GET    /socket        controllers.ContainersController.socket

And I call the socket with :

var exampleSocket = new WebSocket("ws://127.0.0.1:9000/socket");

似乎您正在尝试将WebSocket连接连接到标准的http路由,从而用状态码200回答您。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM