Permalink
Browse files

gh

  • Loading branch information...
0 parents commit b528b69e906158b06b8ffc6bdfc7de2c2a6e24d0 @softprops committed Nov 6, 2010
@@ -0,0 +1,5 @@
+target
+lib_managed
+src_managed
+project/boot
+.DS_Store
@@ -0,0 +1,5 @@
+# Unfiltered Websockets g8 template
+
+a simple g8 template for demoing unfiltered websockets
+
+2010 Doug Tangren (softprops)
@@ -0,0 +1,6 @@
+project.organization=com.example
+project.name=unfiltered websockets
+sbt.version=0.7.4
+project.version=0.1.0
+build.scala.versions=2.7.7
+project.initialize=false
@@ -0,0 +1,6 @@
+import sbt._
+
+class UnfilteredWebSocketsTemplate(info: ProjectInfo)
+ extends DefaultProject(info) with giter8.Template {
+ override def disableCrossPaths = true
+}
@@ -0,0 +1,5 @@
+import sbt._
+
+class Plugins(info: ProjectInfo) extends PluginDefinition(info) {
+ val giter8 = "net.databinder" % "giter8-plugin" % "0.1.0"
+}
@@ -0,0 +1,3 @@
+#Project properties
+#Sat Nov 06 01:03:57 EDT 2010
+plugin.uptodate=true
@@ -0,0 +1,4 @@
+name=My Websocket Project
+unfiltered_version=0.2.2
+websocket_port=5678
+socket_connections=4
@@ -0,0 +1,6 @@
+project.organization=com.example
+project.name=$name$
+sbt.version=0.7.4
+project.version=0.1.0
+build.scala.versions=2.8.0
+project.initialize=false
@@ -0,0 +1,6 @@
+import sbt._
+
+class Project(info: ProjectInfo) extends DefaultProject(info) {
+ val uf_version = "$unfiltered_version$"
+ val ufws = "net.databinder" %% "unfiltered-websockets" % uf_version
+}
@@ -0,0 +1,5 @@
+import sbt._
+
+class Plugins(info: ProjectInfo) extends PluginDefinition(info) {
+ // remove this file if your template doesn't have default plugins
+}
@@ -0,0 +1,80 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>Unfiltered WebSockets</title>
+ <style type="text/css">
+ * { margin:0; padding:0; }
+ body { font-size: 24px; font-family:helvetica, sans serif; padding:1em; color:#333; }
+ input[type='text'] { font-size:24px; } #debug { font-size:16px; color:#ccc; }
+ </style>
+ <script text="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
+ </head>
+ <body>
+ ws://localhost:$websocket_port$/
+ <form onsubmit="return false;">
+ <input type="text" name="message" value="Hello World" />
+ <input type="submit" value="Send" id="submit" />
+ <input type="button" value="toogle connection" id="tooglr" />
+ </form>
+ <div id="debug"></div>
+ <ul></ul>
+ <script type="text/javascript">
+ (function(jq){
+ var socket, host = "ws://localhost:$websocket_port$/";
+
+ var supportsWebsockets = function() { return window.WebSocket; }
+
+ var createSocket = function(uri) {
+ if(supportsWebsockets()) {
+ socket = new WebSocket(uri);
+ socket.onmessage = function(e) {
+ jq('ul').first().append(['<li>', e.data, '</li>'].join(""));
+ }
+ socket.onopen = function(e) {
+ debug('socket open')
+ }
+ socket.onclose = function(e) {
+ debug('socket close');
+ }
+ } else {
+ alert("your browser does not support web sockets. try chrome.");
+ }
+ }
+
+ var debug = function(msg) { jq("#debug").html(msg); }
+
+ createSocket(host);
+
+ var send = function(message) {
+ if(!supportsWebsockets()) { return; }
+ if(socket.readyState == WebSocket.OPEN) {
+ socket.send(message);
+ } else {
+ alert("socket is not open");
+ }
+ }
+ var isOpen = function() { return supportsWebsockets() ? socket.readyState == WebSocket.OPEN : false; }
+ var isClosed = function() { return supportsWebsockets() ? !isOpen() : false; };
+ var closeSocket = function() { if(supportsWebsockets() && isOpen()) { socket.close(); } };
+ var openSocket = function() {
+ if(isOpen()) {
+ alert('socket already open');
+ return;
+ }
+ createSocket(host);
+ }
+ var toggleConnection = function() { if(isOpen()) { closeSocket(); } else { openSocket(); } };
+ jq("#submit").click(function(e){
+ e.preventDefault();
+ send(this.form.message.value);
+ return false;
+ });
+ jq("#tooglr").click(function(e){
+ e.preventDefault();
+ toggleConnection();
+ return false;
+ });
+ })(jQuery);
+ </script>
+ </body>
+</html>
@@ -0,0 +1,25 @@
+object App {
+ import unfiltered.websockets._
+ import unfiltered.util.Browser
+ import scala.collection.mutable.ListBuffer
+
+ def main(args: Array[String]) {
+ (1 to $socket_connections$).foreach { i =>
+ Browser.open("file://%s" format getClass.getResource("client.html").getFile)
+ }
+ val sockets = new ListBuffer[WebSocket]()
+ WebSocketServer("/", $websocket_port$) {
+ case Open(s) =>
+ sockets.foreach(_.send("socket %s joined" format sockets.size + 1))
+ sockets + s
+ s.send("hola!")
+ case Message(s, Text(msg)) =>
+ sockets.foreach(_.send(msg))
+ case Close(s) =>
+ sockets - s
+ println("closed socket %s" format(s))
+ case Error(s, e) =>
+ println("error occured %s" format e.getMessage)
+ }
+ }
+}

0 comments on commit b528b69

Please sign in to comment.