Skip to content

Instantly share code, notes, and snippets.

@mmaz
Created June 24, 2013 04:30
Show Gist options
  • Save mmaz/5847746 to your computer and use it in GitHub Desktop.
Save mmaz/5847746 to your computer and use it in GitHub Desktop.
Some glue-code to demo using a http://square.github.io/cube/ backend via websockets, with an Evaluator client script in Scala, and an Emitter in Dart.
import 'dart:html';
import 'dart:json';
const IP = "1.2.3.4";
const COLLECTOR_PORT = "1080";
void main() {
WebSocket ws = new WebSocket('ws://' + IP + ':' + COLLECTOR_PORT + '/1.0/event/put');
ws.onOpen.listen((_) => print("connected"));
ButtonElement b1 = query("#b1");
ButtonElement b2 = query("#b2");
ws.onError.listen((Event e) => print(e));
ws.onClose.listen((CloseEvent e) => print(e.reason));
b1.onClick.listen((_) => ws.send(stringify({'type' : 'foo'})));
b2.onClick.listen((_) => ws.send(stringify({'type' : 'bar'})));
query("#b3").onClick.listen((_) => print(ws.readyState));
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Cube emitter</title>
</head>
<body>
<button id="b1">send foo</button>
<button id="b2">send bar</button>
<button id="b3">ready state</button>
<script type="application/dart" src="cube_emitter.dart"></script>
<script src="packages/browser/dart.js"></script>
</body>
</html>
import java.net.URI
import org.java_websocket.client.WebSocketClient
import org.java_websocket.handshake.ServerHandshake
import spray.json._
class MazClient(serverUri : URI) extends WebSocketClient(serverUri : URI) {
override def onOpen(handshakedata: ServerHandshake) {
println("opened")
}
override def onMessage(message: String) {
println(message)
}
def onClose(p1: Int, p2: String, p3: Boolean) {
println("closing")
}
def onError(p1: Exception) {
println("uhoh ")
}
}
case class EventGet(expression: String, start: String, id: String, delay: Int)
object CubeJsonProtocol extends DefaultJsonProtocol {
implicit val colorFormat = jsonFormat4(EventGet)
}
object Main extends App {
import CubeJsonProtocol._
private val IP = "1.2.3.4"
private val EVALUATOR_PORT = "1081"
val mc = new MazClient(new URI("ws://" + IP + ":" + EVALUATOR_PORT + "/1.0/event/get"))
mc.connectBlocking()
mc.send(EventGet("foo", "2013-06-23", "foo", 0).toJson.compactPrint)
mc.send(EventGet("bar", "2013-06-23", "bar", 0).toJson.compactPrint)
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>scala_websocket_client</groupId>
<artifactId>scala_websocket_client</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<scala.version>2.10.2</scala.version>
</properties>
<repositories>
<repository>
<id>sprayrepo</id>
<name>spray repo</name>
<url>http://repo.spray.io/</url>
<layout>default</layout>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>io.spray</groupId>
<artifactId>spray-json_2.10</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>14.0</version>
</dependency>
<dependency>
<groupId>org.java-websocket</groupId>
<artifactId>Java-WebSocket</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>org.scalatest</groupId>
<artifactId>scalatest_2.10</artifactId>
<version>2.0.M5b</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/scala</sourceDirectory>
<testSourceDirectory>src/test/scala</testSourceDirectory>
<pluginManagement>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.1.5</version>
<configuration>
<scalaVersion>${scala.version}</scalaVersion>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.1.5</version>
</plugin>
</plugins>
</build>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment