Ludo
Ludo

Reputation: 157

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

Upvotes: 2

Views: 354

Answers (1)

mutantacule
mutantacule

Reputation: 7063

It looks like you are trying to connect with your WebSocket connection to a standard http route, which answers you with status code 200.

Upvotes: 0

Related Questions