From d31b2f6a92e30e8038535c7c1670620cfa6b6714 Mon Sep 17 00:00:00 2001 From: Thibault Duplessis Date: Tue, 27 Aug 2019 19:40:05 +0200 Subject: [PATCH] ping --- .gitignore | 3 +++ app/SiteClientActor.scala | 22 +++++++++++++++++ app/controllers/SocketController.scala | 31 ++++++++++++++++++++++++ build.sbt | 17 +++++++++++++ conf/application.conf | 23 ++++++++++++++++++ conf/logback.xml | 33 ++++++++++++++++++++++++++ conf/routes | 5 ++++ project/build.properties | 1 + project/plugins.sbt | 2 ++ 9 files changed, 137 insertions(+) create mode 100644 .gitignore create mode 100644 app/SiteClientActor.scala create mode 100644 app/controllers/SocketController.scala create mode 100644 build.sbt create mode 100644 conf/application.conf create mode 100644 conf/logback.xml create mode 100644 conf/routes create mode 100644 project/build.properties create mode 100644 project/plugins.sbt diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..8ea72b57 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +logs/ +target/ +RUNNING_PID diff --git a/app/SiteClientActor.scala b/app/SiteClientActor.scala new file mode 100644 index 00000000..980e8d7a --- /dev/null +++ b/app/SiteClientActor.scala @@ -0,0 +1,22 @@ +package lichess.ws + +import akka.actor._ +import play.api.libs.json._ + +final class SiteClientActor(out: ActorRef) extends Actor { + + def receive = { + case Ping => send(Pong) + case msg: JsValue => send(JsString("Got: " + msg)) + } + + val Ping = JsNull + val Pong = JsNumber(0) + + def send(msg: JsValue) = out ! msg +} + +object SiteClientActor { + + def props(out: ActorRef) = Props(new SiteClientActor(out)) +} diff --git a/app/controllers/SocketController.scala b/app/controllers/SocketController.scala new file mode 100644 index 00000000..fa512c73 --- /dev/null +++ b/app/controllers/SocketController.scala @@ -0,0 +1,31 @@ +package controllers + +import javax.inject._ + +import akka.actor._ +import akka.event.Logging +import akka.stream.Materializer +import play.api.libs.streams.ActorFlow +import play.api.Logger +import play.api.mvc._ +import play.api.libs.json._ + +import scala.concurrent.{ ExecutionContext, Future } + +import lichess.ws._ + +@Singleton +class SocketController @Inject() (val controllerComponents: ControllerComponents)(implicit + actorSystem: ActorSystem, + mat: Materializer, + executionContext: ExecutionContext +) extends BaseController { + + private type WSMessage = JsValue + private val logger = Logger(getClass) + + def site(): WebSocket = + WebSocket.accept[WSMessage, WSMessage] { req => + ActorFlow actorRef { out => SiteClientActor.props(out) } + } +} diff --git a/build.sbt b/build.sbt new file mode 100644 index 00000000..f3106e22 --- /dev/null +++ b/build.sbt @@ -0,0 +1,17 @@ +name := "lila-ws" + +version := "1.0-SNAPSHOT" + +lazy val root = (project in file(".")).enablePlugins(PlayScala).disablePlugins(PlayFilters) + +/* val akkaVersion = "2.6.0-M2" */ + +scalaVersion := "2.13.0" + +libraryDependencies += guice + +scalacOptions ++= Seq( + "-feature", + "-deprecation", + "-Xfatal-warnings" +) diff --git a/conf/application.conf b/conf/application.conf new file mode 100644 index 00000000..017ed6e6 --- /dev/null +++ b/conf/application.conf @@ -0,0 +1,23 @@ +# Uncomment this for the most verbose Akka debugging: +akka { + loggers = ["akka.event.slf4j.Slf4jLogger"] + loglevel = "DEBUG" + logging-filter = "akka.event.slf4j.Slf4jLoggingFilter" + + actor { + debug { + #receive = on + #autoreceive = on + #lifecycle = on + } + } +} + + +play.filters.headers.contentSecurityPolicy = null + +# https://www.playframework.com/documentation/latest/AllowedHostsFilter +# Allow requests to localhost:9000. +play.filters.hosts { + allowed = ["localhost:9000"] +} diff --git a/conf/logback.xml b/conf/logback.xml new file mode 100644 index 00000000..970fc688 --- /dev/null +++ b/conf/logback.xml @@ -0,0 +1,33 @@ + + + + + + logs/application.log + + %date [%level] from %logger in %thread - %message%n%xException + + + + + + %coloredLevel %logger{15} - [%marker] %message%n%xException{10} + + + + + + + + + + + + + + + + + + + diff --git a/conf/routes b/conf/routes new file mode 100644 index 00000000..6bfaa053 --- /dev/null +++ b/conf/routes @@ -0,0 +1,5 @@ +# Routes +# This file defines all application routes (Higher priority routes first) +# ~~~~ + +GET /socket/v4 controllers.SocketController.site diff --git a/project/build.properties b/project/build.properties new file mode 100644 index 00000000..c0bab049 --- /dev/null +++ b/project/build.properties @@ -0,0 +1 @@ +sbt.version=1.2.8 diff --git a/project/plugins.sbt b/project/plugins.sbt new file mode 100644 index 00000000..9db9c46b --- /dev/null +++ b/project/plugins.sbt @@ -0,0 +1,2 @@ +// Use the Play sbt plugin for Play projects +addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.7.3")