typesafehub / play-eventsource-clock-java8

Activator template for the EventSource Clock sample for Java 8
Other
2 stars 4 forks source link

help with scala port? #2

Open matanox opened 8 years ago

matanox commented 8 years ago

Not a bug or anything, but I started porting this to Scala, and got really slow on finding the equivalent Scala api for the core part. Any help pointing me at the right direction would be very nice, if possible :-)

package controllers

import java.util._
import java.text._
import scala.concurrent.duration.Duration
import java.util.concurrent.TimeUnit._
import play.libs.EventSource.Event.event
import views.html._
import akka.actor._
import play.api.mvc._
import play.api.libs.json.JsValue
import play.api.libs.iteratee._
import play.api.libs.concurrent.Execution.Implicits._
import play.api.libs.EventSource.EventDataExtractor
import play.libs.EventSource
import play.api.Logger

object Demo extends Controller {

    private val clock = new Clock

    def home() = Action {
      Ok(index.render());
    }

    def liveClock() = Action {
      Ok(EventSource.whenConnected(es => clock.tell(es, null)));
    }

    class Clock extends UntypedActor {

        val instance = system.actorOf(Props[Clock])

        val system = ActorSystem()

        system.scheduler.schedule(Duration.Zero, Duration.create(100, MILLISECONDS))(instance, "TICK") 

        val dateFormat = new SimpleDateFormat("HH mm ss");

        val sockets = new ArrayList[EventSource]

        def onReceive(message: Any) {

          // Handle connections
          message match {
            case EventSource(eventSource) =>

                if (sockets.contains(eventSource)) {
                    // Browser is disconnected
                    sockets.remove(eventSource);
                    Logger.info("Browser disconnected (" + sockets.size() + " browsers currently connected)");

                } else {
                    // Register disconnected callback
                    eventSource.onDisconnected(() -> self().tell(eventSource, null));
                    // New browser connected
                    sockets.add(eventSource);
                    Logger.info("New browser connected (" + sockets.size() + " browsers currently connected)");

                }              

            case s: String if s == "TICK" =>
              for (EventSource es : sockets) {
                es.send(event(dateFormat.format(new Date())));
              }

        }

    }

}

Do we really have to use Enumerators as per here, to get this going in Scala? I am probably not importing the right library for Enumerator.imperative if that's the case... but overall I find it rather confusing whereas the api source code is riddled with a lot of deprecation.

Many thanks if this has a simple solution!

wsargent commented 8 years ago

Hi -- this should be typesafehub/play-2.5-clock

Will Sargent

On March 7, 2016 at 1:49:34 AM, matanster (notifications@github.com) wrote:

Not a bug or anything, but I started porting this to Scala, and got really slow on finding the equivalent Scala api for the core part. Any help pointing me at the right direction would be very nice, if possible :-)

`package controllers

import java.util. import java.text. import scala.concurrent.duration.Duration import java.util.concurrent.TimeUnit. import play.libs.EventSource.Event.event import views.html. import akka.actor. import play.api.mvc. import play.api.libs.json.JsValue import play.api.libs.iteratee.{Concurrent, Enumeratee} import play.api.libs.concurrent.Execution.Implicits._ import play.api.libs.EventSource.EventDataExtractor import play.libs.EventSource import play.api.Logger

object Demo extends Controller {

private val clock = Clock.instance

def home() = Action { Ok(index.render()); }

def liveClock() = Action { Ok(EventSource.whenConnected(es => clock.tell(es, null))); }

class Clock extends UntypedActor {

val instance = system.actorOf(Props[Clock])

val system = ActorSystem()

system.scheduler.schedule(Duration.Zero, Duration.create(100, MILLISECONDS))(instance, "TICK")

val dateFormat = new SimpleDateFormat("HH mm ss");

val sockets = new ArrayList[EventSource]

def onReceive(message: Any) {

  // Handle connections
  message match {
    case EventSource(eventSource) =>

        if (sockets.contains(eventSource)) {
            // Browser is disconnected
            sockets.remove(eventSource);
            Logger.info("Browser disconnected (" + sockets.size() + " browsers currently connected)");

        } else {
            // Register disconnected callback
            eventSource.onDisconnected(() -> self().tell(eventSource, null));
            // New browser connected
            sockets.add(eventSource);
            Logger.info("New browser connected (" + sockets.size() + " browsers currently connected)");

        }

    case s: String if s == "TICK" =>
      for (EventSource es : sockets) {
        es.send(event(dateFormat.format(new Date())));
      }

}

}

} `

— Reply to this email directly or view it on GitHub https://github.com/typesafehub/play-eventsource-clock-java8/issues/2.

matanox commented 8 years ago

Thanks! this wouldn't come up in google for me... maybe because it's relatively new. Again many thanks.

On Mon, Mar 7, 2016 at 5:22 PM, Will Sargent notifications@github.com wrote:

Hi -- this should be typesafehub/play-2.5-clock

Will Sargent

On March 7, 2016 at 1:49:34 AM, matanster (notifications@github.com) wrote:

Not a bug or anything, but I started porting this to Scala, and got really slow on finding the equivalent Scala api for the core part. Any help pointing me at the right direction would be very nice, if possible :-)

`package controllers

import java.util. import java.text. import scala.concurrent.duration.Duration import java.util.concurrent.TimeUnit. import play.libs.EventSource.Event.event import views.html. import akka.actor. import play.api.mvc. import play.api.libs.json.JsValue import play.api.libs.iteratee.{Concurrent, Enumeratee} import play.api.libs.concurrent.Execution.Implicits._ import play.api.libs.EventSource.EventDataExtractor import play.libs.EventSource import play.api.Logger

object Demo extends Controller {

private val clock = Clock.instance

def home() = Action { Ok(index.render()); }

def liveClock() = Action { Ok(EventSource.whenConnected(es => clock.tell(es, null))); }

class Clock extends UntypedActor {

val instance = system.actorOf(Props[Clock])

val system = ActorSystem()

system.scheduler.schedule(Duration.Zero, Duration.create(100, MILLISECONDS))(instance, "TICK")

val dateFormat = new SimpleDateFormat("HH mm ss");

val sockets = new ArrayList[EventSource]

def onReceive(message: Any) {

// Handle connections message match { case EventSource(eventSource) =>

if (sockets.contains(eventSource)) { // Browser is disconnected sockets.remove(eventSource); Logger.info("Browser disconnected (" + sockets.size() + " browsers currently connected)");

} else { // Register disconnected callback eventSource.onDisconnected(() -> self().tell(eventSource, null)); // New browser connected sockets.add(eventSource); Logger.info("New browser connected (" + sockets.size() + " browsers currently connected)");

}

case s: String if s == "TICK" => for (EventSource es : sockets) { es.send(event(dateFormat.format(new Date()))); }

}

}

} `

— Reply to this email directly or view it on GitHub https://github.com/typesafehub/play-eventsource-clock-java8/issues/2.

— Reply to this email directly or view it on GitHub https://github.com/typesafehub/play-eventsource-clock-java8/issues/2#issuecomment-193296456 .