-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
much more work on an example, I think I know what we could do
- Loading branch information
Showing
10 changed files
with
181 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/* The main css of this app */ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>Wikipedia Update Dashboard</title> | ||
<link rel="stylesheet" type="text/css" href="css/app.css"> | ||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> | ||
<script type="application/javascript" src="js/app.js"></script> | ||
</head> | ||
<body> | ||
<h1>Wikipedia Updates</h1> | ||
<ul class="wikipedia-updates"> | ||
</ul> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
$(function() { | ||
|
||
if (!window.WebSocket) { | ||
window.WebSocket = window.MozWebSocket; | ||
} | ||
|
||
if (window.WebSocket) { | ||
var l = window.location; | ||
var wsUrl = ((l.protocol === "https:") ? "wss://" : "ws://") + l.hostname + (((l.port != 80) && (l.port != 443)) ? ":" + l.port : "") + l.pathname + "socket"; | ||
var socket = new WebSocket(wsUrl); | ||
socket.onmessage = function (event) { | ||
$(".wikipedia-updates").append('<li>' + event.data + '</li>'); | ||
}; | ||
socket.onopen = function (_event) { | ||
console.log("ready to rumble :)"); | ||
}; | ||
|
||
} else { | ||
console.log("You don't have websockets :(") | ||
} | ||
|
||
}); |
41 changes: 41 additions & 0 deletions
41
example/scala/io/trsc/reactive/irc/example/ConnectionHandler.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package io.trsc.reactive.irc.example | ||
|
||
import akka.actor.{ActorRef, ActorSystem, Props} | ||
import akka.http.scaladsl.model.ws.{Message, TextMessage} | ||
import akka.http.scaladsl.server.Directives | ||
import akka.stream.OverflowStrategy | ||
import akka.stream.scaladsl._ | ||
|
||
object ConnectionHandler { | ||
def apply(broadcastSource: Source[BroadcastMessage, _])(implicit sys: ActorSystem) = new ConnectionHandler(broadcastSource).flow | ||
} | ||
|
||
class ConnectionHandler(broadcastSource: Source[BroadcastMessage, _])(implicit sys: ActorSystem) extends Directives { | ||
|
||
private val connectionSupervisor = sys.actorOf(Props[ConnectionSupervisor]) | ||
|
||
def flow = Flow(wrappedSupervisorSink, clientOutSource)(Keep.right) { implicit builder => | ||
(supervisor, out) => | ||
import akka.stream.scaladsl.FlowGraph.Implicits._ | ||
|
||
val broadcast = builder.add(broadcastSource) | ||
val collectTextMessage = builder.add(collectTextMessageFlow) | ||
val convertToIncomingMessage = builder.add(convertToIncomingMessageFlow) | ||
val merge = builder.add(Merge[ConnectionEvent](3)) | ||
|
||
broadcast ~> merge.in(0) | ||
collectTextMessage ~> convertToIncomingMessage ~> merge.in(1) | ||
builder.matValue ~> convertActorRefToNewConnection ~> merge.in(2); merge ~> supervisor | ||
|
||
(collectTextMessage.inlet, out.outlet) | ||
} | ||
|
||
private val wrappedSupervisorSink = Sink.actorRef[ConnectionEvent](connectionSupervisor, ConnectionClosed) | ||
private val clientOutSource = Source.actorRef[Message](1, OverflowStrategy.fail) | ||
|
||
private val collectTextMessageFlow = Flow[Message] collect { case TextMessage.Strict(msg) => msg } | ||
private val convertToIncomingMessageFlow = Flow[String] map { IncomingMessage } | ||
|
||
private val convertActorRefToNewConnection = Flow[ActorRef] map { NewConnection } | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
example/scala/io/trsc/reactive/irc/example/ConnectionSupervisor.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package io.trsc.reactive.irc.example | ||
|
||
import akka.actor.{Terminated, ActorRef, Actor} | ||
import akka.http.scaladsl.model.ws.TextMessage | ||
|
||
sealed trait ConnectionEvent | ||
case object ConnectionClosed extends ConnectionEvent | ||
case class NewConnection(actorRef: ActorRef) extends ConnectionEvent | ||
case class IncomingMessage(msg: String) extends ConnectionEvent | ||
case class BroadcastMessage(msg: String) extends ConnectionEvent | ||
|
||
class ConnectionSupervisor extends Actor { | ||
|
||
var subscribers = Set.empty[ActorRef] | ||
|
||
def receive = { | ||
case NewConnection(subscriber) => | ||
context.watch(subscriber) | ||
subscribers += subscriber | ||
println("subscription added") | ||
case BroadcastMessage(msg) => | ||
subscribers foreach { _ ! TextMessage.Strict(msg) } | ||
println("broadcasted: " + msg) | ||
case IncomingMessage(_) => println("TODO: we could have the client register for diff languages") | ||
case ConnectionClosed => println("TODO: proper logging") | ||
case Terminated(subscriber) => | ||
subscribers -= subscriber | ||
println("subscription terminated") | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package io.trsc.reactive.irc.example | ||
|
||
import akka.http.scaladsl.model.ws.Message | ||
import akka.http.scaladsl.server.Directives | ||
import akka.stream.scaladsl.Flow | ||
|
||
object Routes extends Directives { | ||
|
||
def apply(handler: Flow[Message, Message, Any]) = | ||
get { | ||
pathSingleSlash { | ||
getFromResource("web/index.html") | ||
} ~ | ||
path("socket") { | ||
handleWebsocketMessages(handler) | ||
} ~ | ||
getFromResourceDirectory("web") | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
example/scala/io/trsc/reactive/irc/example/WikipediaUpdatesServer.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package io.trsc.reactive.irc.example | ||
|
||
import akka.actor.ActorSystem | ||
import akka.http.scaladsl.Http | ||
import akka.stream.ActorFlowMaterializer | ||
import io.trsc.reactive.irc.ReactiveIRC | ||
import io.trsc.reactive.irc.protocol.IrcMessage | ||
|
||
import scala.util.{Failure, Success} | ||
|
||
object WikipediaUpdatesServer extends App { | ||
|
||
implicit val system = ActorSystem("wikipedia-updates-system") | ||
import system.dispatcher | ||
implicit val materializer = ActorFlowMaterializer() | ||
|
||
val broadcastSource = ReactiveIRC.listen("irc.wikimedia.org", 6667, "reactive-example", "#en.wikipedia" :: Nil) collect { | ||
case IrcMessage(_, _, params) => params.last | ||
} map { BroadcastMessage } | ||
|
||
val interface = "localhost" | ||
val port = args.headOption.map(_.toInt).getOrElse(7117) | ||
|
||
Http().bindAndHandle(Routes(ConnectionHandler(broadcastSource)), interface, port).onComplete { | ||
case Success(binding) ⇒ | ||
val localAddress = binding.localAddress | ||
println(s"Started WikipediaUpdatesServer on ${localAddress.getHostName}:${localAddress.getPort}") | ||
case Failure(e) ⇒ | ||
println(s"Failed to start WikipediaUpdatesServer with ${e.getMessage}") | ||
system.shutdown() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters