Safer socket connection

pull/1/merge
Thibault Duplessis 2012-04-21 12:41:46 +02:00
parent 24d28fbfea
commit 136e91e936
7 changed files with 53 additions and 25 deletions

View File

@ -20,7 +20,7 @@ object AppC extends LilaController {
def socket = WebSocket.async[JsValue] { implicit request
env.siteSocket.join(
uid = get("uid") err "Socket UID missing",
uidOption = get("uid"),
username = get("username"))
}
@ -29,8 +29,8 @@ object AppC extends LilaController {
env.gameSocket.join(
gameId = gameId,
colorName = color,
uid = get("uid") err "Socket UID missing",
version = getInt("version") err "Socket version missing",
uidOption = get("uid"),
versionOption = getInt("version"),
playerId = get("playerId")).unsafePerformIO
}

View File

@ -19,8 +19,8 @@ object LobbyC extends LilaController {
def socket = WebSocket.async[JsValue] { implicit request
env.lobbySocket.join(
uid = get("uid") err "Socket UID missing",
version = getInt("version") err "Socket version missing",
uidOption = get("uid"),
versionOption = getInt("version"),
hook = get("hook")
)
}

View File

@ -15,6 +15,7 @@ import scalaz.effects._
import chess.Color
import model.{ DbGame, Pov, PovRef, Progress, Event }
import socket.Util
import RichJs._
final class Socket(
@ -67,13 +68,15 @@ final class Socket(
def join(
gameId: String,
colorName: String,
uid: String,
version: Int,
uidOption: Option[String],
versionOption: Option[Int],
playerId: Option[String]): IO[SocketPromise] =
getGame(gameId) map { gameOption
val promise: Option[SocketPromise] = for {
game gameOption
color Color(colorName)
uid uidOption
version versionOption
hub = hubMemo get gameId
} yield (hub ? Join(
uid = uid,
@ -90,7 +93,7 @@ final class Socket(
},
member.channel)
}
promise | connectionFail
promise | Util.connectionFail
}
private def scheduleForDeletion(hub: ActorRef, gameId: String) {
@ -98,10 +101,4 @@ final class Socket(
hub ! IfEmpty(hubMemo remove gameId)
}
}
private def connectionFail = Promise.pure {
Done[JsValue, Unit]((), Input.EOF) -> (Enumerator[JsValue](
JsObject(Seq("error" -> JsString("Invalid request")))
) andThen Enumerator.enumInput(Input.EOF))
}
}

View File

@ -11,16 +11,20 @@ import play.api.libs.concurrent._
import scalaz.effects._
import RichJs._
import socket.Util
final class Socket(hub: ActorRef) {
implicit val timeout = Timeout(1 second)
def join(
uid: String,
version: Int,
hook: Option[String]): SocketPromise =
(hub ? Join(uid, version, hook)).asPromise map {
uidOption: Option[String],
versionOption: Option[Int],
hook: Option[String]): SocketPromise = {
val socket = for {
uid uidOption
version versionOption
} yield (hub ? Join(uid, version, hook)).asPromise map {
case Connected(channel)
val iteratee = Iteratee.foreach[JsValue] { e
e str "t" match {
@ -35,7 +39,9 @@ final class Socket(hub: ActorRef) {
hub ! Quit(uid)
}
(iteratee, channel)
}
}: SocketPromise
socket | Util.connectionFail
}
def addEntry(entry: model.Entry): IO[Unit] = io {
hub ! Entry(entry)

View File

@ -5,20 +5,23 @@ import akka.actor._
import akka.pattern.ask
import akka.util.duration._
import akka.util.Timeout
import play.api._
import play.api.libs.json._
import play.api.libs.iteratee._
import play.api.libs.concurrent._
import scalaz.effects._
import socket.Util
final class Socket(hub: ActorRef) {
implicit val timeout = Timeout(1 second)
def join(uid: String, username: Option[String]): SocketPromise =
(hub ? Join(uid, username)).asPromise map {
def join(
uidOption: Option[String],
username: Option[String]): SocketPromise = {
val socket = for {
uid uidOption
} yield (hub ? Join(uid, username)).asPromise map {
case Connected(channel)
val iteratee = Iteratee.foreach[JsValue] { _
Unit
@ -26,5 +29,7 @@ final class Socket(hub: ActorRef) {
hub ! Quit(uid)
}
(iteratee, channel)
}
} : SocketPromise
socket | Util.connectionFail
}
}

View File

@ -0,0 +1,16 @@
package lila
package socket
import play.api.libs.concurrent._
import play.api.libs.json._
import play.api.libs.iteratee._
object Util {
val connectionFail: SocketPromise = Promise.pure {
Done[JsValue, Unit]((), Input.EOF) -> (Enumerator[JsValue](
JsObject(Seq("error" -> JsString("Invalid request")))
) andThen Enumerator.enumInput(Input.EOF))
}
}

4
todo
View File

@ -1 +1,5 @@
ping nbm
games played scrolling
chat username case
back button
all players ()