Skip to content
This repository has been archived by the owner on Jan 22, 2022. It is now read-only.

Connect to openvidu #100

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ lazy val playCommonSettings = Seq(
}
)

lazy val domain = (project in file("jam-domain"))
.settings(commonSettings)

lazy val application = (project in file("jam-application"))
.settings(commonSettings)
.dependsOn(domain)

lazy val server = (project in file("jam-server"))
.enablePlugins(PlayScala)
.settings(commonSettings)
Expand All @@ -68,6 +75,9 @@ lazy val server = (project in file("jam-server"))
.settings(
devSettings := Map("play.server.http.port" -> "9000").toSeq
)
.dependsOn(domain)
.dependsOn(application)
.dependsOn(infrastructure)

lazy val infrastructure = (project in file("jam-infrastructure"))
.settings(commonSettings)
Expand All @@ -81,13 +91,18 @@ lazy val infrastructure = (project in file("jam-infrastructure"))
)
}
)
.dependsOn(domain)
.dependsOn(application)

lazy val root = (project in file("."))
.settings(commonSettings)
.settings(
Compile / run := (server / Compile / run).evaluated
)
.aggregate(server, infrastructure)
.aggregate(domain)
.aggregate(application)
.aggregate(infrastructure)
.aggregate(server)

addCommandAlias("fixAll", "scalafixAll; scalafmtAll; scalafmtSbt")
addCommandAlias("checkAll", "scalafixAll --check; scalafmtCheckAll; scalafmtSbtCheck")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package jam.application.effects.http

sealed trait Credential
case class BasicCredential(username: String, password: String) extends Credential
Original file line number Diff line number Diff line change
@@ -1,31 +1,26 @@
package jam.infrastructure.http
package jam.application.effects.http

import io.circe._
import org.atnos.eff._

case class HttpParam(url: String, credential: Credential)

sealed trait Credential
case class BasicCredential(username: String, password: String) extends Credential

sealed trait HttpDSL[+A]
case class Get[A](param: HttpParam, decoder: Decoder[A]) extends HttpDSL[A]
sealed trait HttpEffect[+A]
case class Get[A](param: HttpParam, decoder: Decoder[A]) extends HttpEffect[A]
case class Post[A1, A2](
param: HttpParam,
data: A1,
encoder: Encoder[A1],
decoder: Decoder[A2]
) extends HttpDSL[A2]
) extends HttpEffect[A2]

object HttpDSL {
type _http[R] = HttpDSL |= R
object HttpEffect {
type _http[R] = HttpEffect |= R

def get[A, R: _http](param: HttpParam)(implicit decoder: Decoder[A]): Eff[R, A] =
Eff.send[HttpDSL, R, A](Get(param, decoder))
Eff.send[HttpEffect, R, A](Get(param, decoder))

def post[A1, A2, R: _http](param: HttpParam, data: A1)(implicit
encoder: Encoder[A1],
decoder: Decoder[A2]
): Eff[R, A2] =
Eff.send[HttpDSL, R, A2](Post(param, data, encoder, decoder))
Eff.send[HttpEffect, R, A2](Post(param, data, encoder, decoder))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package jam.application.effects.http

case class HttpParam(url: String, credential: Credential)
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package jam.application.services

import jam.domain.models.{Session, Token}

trait WebRtcService {
def createSession(id: String): Session
def getToken(session: Session): Token
}

object WebRtcService {

// def createSession(id: String): ZIO[WebRtcService, Nothing, Session] =
// ZIO.accessM(env => IO.effectTotal(env.createSession(id)))
//
// def getToken(session: Session): ZIO[WebRtcService, Nothing, Token] =
// ZIO.accessM(env => IO.effectTotal(env.getToken(session)))
}
18 changes: 13 additions & 5 deletions jam-client/src/components/pages/Lobby.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React, { useState, useEffect, useRef } from 'react';
import { Stage } from '@inlet/react-pixi';
import Sockette from 'sockette';
import { Myself, Participant } from 'components/molecules';
import { Myself, Participant } from '../molecules';
import { Position } from 'models';
import { OpenVidu, Session } from 'openvidu-browser';

interface Props {
userName: string;
Expand Down Expand Up @@ -30,6 +31,9 @@ export const Lobby: React.FC<Props> = ({ userName }) => {
const [users, setUsers] = useState<User[]>([]);
const [message, setMessage] = useState<string>('');
const [isTyping, setIsTyping] = useState<boolean>(false);

const ovRef = useRef<OpenVidu>();
const sessionRef = useRef<Session>();
const wsRef = useRef<Sockette>();
const inputRef = useRef<HTMLInputElement>(null);

Expand Down Expand Up @@ -106,6 +110,9 @@ export const Lobby: React.FC<Props> = ({ userName }) => {

document.addEventListener('keydown', keyDown);

ovRef.current = new OpenVidu();
sessionRef.current = ovRef.current.initSession();

const interval = setInterval(() => {
if (wsRef.current)
wsRef.current.send(
Expand All @@ -117,12 +124,12 @@ export const Lobby: React.FC<Props> = ({ userName }) => {

return () => {
console.log('Disconnecting..');

clearInterval(interval);

if (wsRef.current) wsRef.current.close();

document.removeEventListener('keydown', keyDown);
if (sessionRef.current) sessionRef.current.disconnect();
if (ovRef.current) ovRef.current = undefined;
if (wsRef.current) wsRef.current.close();
if (wsRef.current) wsRef.current.close();
};
}, []);

Expand Down Expand Up @@ -165,6 +172,7 @@ export const Lobby: React.FC<Props> = ({ userName }) => {
if (userName != user.name) {
return (
<Participant
key={user.name}
width={50}
height={50}
fontSize={16}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package domain.models
package jam.domain.models

case class Room(name: RoomName, users: Set[User])

Expand Down
3 changes: 3 additions & 0 deletions jam-domain/src/main/scala/jam/domain/models/Session.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package jam.domain.models

case class Session(id: String) extends AnyVal
3 changes: 3 additions & 0 deletions jam-domain/src/main/scala/jam/domain/models/Token.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package jam.domain.models

case class Token()
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package domain.models
package jam.domain.models

case class User(name: UserName, position: Position)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package domain.models
package jam.domain.models

sealed trait UserCommand

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@ import org.atnos.eff.all.fromEither
import sttp.client3.circe._
import sttp.client3.{ResponseException, _}

import jam.application.effects.http._

object SttpHttpInterpreter {

type _either[R] = Either[ResponseException[String, Error], *] |= R

def run[R, U, A](
effects: Eff[R, A]
)(implicit m: Member.Aux[HttpDSL, R, U], either: _either[U]): Eff[U, A] =
translate(effects)(new Translate[HttpDSL, U] {
)(implicit m: Member.Aux[HttpEffect, R, U], either: _either[U]): Eff[U, A] =
translate(effects)(new Translate[HttpEffect, U] {
private val backend = HttpURLConnectionBackend()

override def apply[X](kv: HttpDSL[X]): Eff[U, X] = {
override def apply[X](kv: HttpEffect[X]): Eff[U, X] = {
val result = kv match {
case Get(param, decoder) =>
get(param)(decoder).send(backend).body
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package jam.infrastructure.openvidu

//import io.circe._
//
//import java.time.{Instant, ZonedDateTime}
//import scala.util.Try

object CirceJsonFormats {
// implicit val decodeZonedDateTim: Decoder[ZonedDateTime] = Decoder.decodeZonedDateTime.emapTry {
// str =>
// Try(Instant.parse(str))
// }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package jam.infrastructure.openvidu

//import jam.application.services.WebRtcService
//import jam.domain.models.{Session, Token}
//import sttp.client3._
//import sttp.client3.circe._
//import io.circe.generic.auto._
//import sttp.client3.asynchttpclient.zio._

import java.time.ZonedDateTime

object WebRtcServiceOnOpenVidu {
// import CirceJsonFormats._

// private val resource = s"$baseUrl/api/sessions"

// def live(baseUrl: String, username: String, password: String): WebRtcService =
// new WebRtcService {
// private val request = basicRequest.auth.basic(username, password)
//
// override def createSession(id: String): Session = {
// val req = request
// .post(uri"$resource")
// .body(InitializeSession(id))
// .response(asJson[InitializedSession])
//
// send(req)
// }
//
// override def getToken(session: Session): Token =
// ???
// }
}

private[openvidu] case class InitializeSession(customSessionId: String)
private[openvidu] case class InitializedSession(id: String, createdAt: ZonedDateTime)
3 changes: 2 additions & 1 deletion jam-server/app/actors/RoomRequestActor.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package actors

import akka.actor.{Actor, ActorRef, Props}
import domain.models.{Position, Room, RoomName, User, UserCommand, UserName}
import infrastructure.RedisClient
import play.api.libs.json.{JsValue, Json}

import jam.domain.models.{Position, Room, RoomName, User, UserCommand, UserName}

class RoomRequestActor(out: ActorRef, redis: RedisClient, roomName: RoomName, userName: UserName)
extends Actor {
import formatters.PlayJsonFormats._
Expand Down
5 changes: 3 additions & 2 deletions jam-server/app/actors/RoomResponseActor.scala
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package actors

import akka.actor.{Actor, ActorRef, PoisonPill, Props}
import domain.models.{User, UserName}
import infrastructure.RedisClient
import play.api.libs.json.Json

import jam.domain.models.{User, UserName}

class RoomResponseActor(out: ActorRef, redisClient: RedisClient, myself: UserName) extends Actor {
import formatters.PlayJsonFormats._
import domain.models.UserCommand._
import jam.domain.models.UserCommand._

private val logger = play.api.Logger(getClass)

Expand Down
3 changes: 2 additions & 1 deletion jam-server/app/controllers/WebSocketsController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import javax.inject._
import actors.{RoomRequestActor, RoomResponseActor}
import akka.actor.ActorSystem
import akka.stream.scaladsl._
import domain.models.{RoomName, UserCommand, UserName}
import infrastructure.RedisClient
import play.api.libs.json.JsValue
import play.api.libs.streams.ActorFlow
import play.api.mvc._
import services.RoomService

import jam.domain.models.{RoomName, UserCommand, UserName}

@Singleton
class WebSocketsController @Inject() (
val controllerComponents: ControllerComponents,
Expand Down
3 changes: 2 additions & 1 deletion jam-server/app/formatters/PlayJsonFormats.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package formatters

import domain.models.{Position, User, UserCommand, UserName}
import play.api.libs.functional.syntax._
import play.api.libs.json._

import jam.domain.models.{Position, User, UserCommand, UserName}

object PlayJsonFormats {
import UserCommand._

Expand Down
3 changes: 2 additions & 1 deletion jam-server/app/infrastructure/RoomChannel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package infrastructure

import akka.NotUsed
import akka.stream.scaladsl.{Sink, Source}
import domain.models.UserCommand

import jam.domain.models.UserCommand

case class RoomChannel(
sink: Sink[UserCommand, NotUsed],
Expand Down
3 changes: 2 additions & 1 deletion jam-server/app/infrastructure/RoomRepositoryOnMemory.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import javax.inject.{Inject, Singleton}
import akka.actor.ActorSystem
import akka.stream.scaladsl.{BroadcastHub, Flow, Keep, MergeHub, Sink}
import akka.stream.{KillSwitches, Materializer, UniqueKillSwitch}
import domain.models.{Room, RoomName, UserCommand, UserName}
import services.{RoomInfo, RoomRepository}

import jam.domain.models.{Room, RoomName, UserCommand, UserName}

import scala.collection.mutable
import scala.concurrent.duration._

Expand Down
3 changes: 2 additions & 1 deletion jam-server/app/services/RoomRepository.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package services

import akka.stream.UniqueKillSwitch
import akka.stream.scaladsl.Flow
import domain.models.{Room, RoomName, UserCommand, UserName}

import jam.domain.models.{Room, RoomName, UserCommand, UserName}

case class RoomInfo(room: Room, bus: Flow[UserCommand, UserCommand, UniqueKillSwitch])

Expand Down
2 changes: 1 addition & 1 deletion jam-server/app/services/RoomService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package services

import javax.inject.{Inject, Singleton}

import domain.models.{RoomName, UserName}
import jam.domain.models.{RoomName, UserName}

@Singleton
class RoomService @Inject() (roomRepository: RoomRepository) {
Expand Down
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.5.5
sbt.version=1.5.5