jfields$ lein new ws-intro Created new project in: /Users/jfields/src/ws-intro Look over project.clj and start coding in ws_intro/core.clj

(defproject ws-intro "1.0.0-SNAPSHOT" :description "FIXME: write description" :dependencies [[org.clojure/clojure "1.3.0"] [org.webbitserver/webbit "0.4.3"] [org.clojure/data.json "0.1.2"]])

-rw-r--r-- 1 jfields domain.users 3390414 Feb 20 15:25 clojure-1.3.0.jar -rw-r--r-- 1 jfields domain.users 5177 Feb 20 15:25 data.json-0.1.2.jar -rw-r--r-- 1 jfields domain.users 801614 Feb 20 15:25 netty-3.2.7.Final.jar -rw-r--r-- 1 jfields domain.users 154735 Feb 20 15:25 webbit-0.4.3.jar

(ns ws-intro.core (:require [clojure.data.json :as json] [clojure.string :as s]) (:import [org.webbitserver WebServer WebServers WebSocketHandler] [org.webbitserver.handler StaticFileHandler])) (defn -main [] (doto (WebServers/createWebServer 8080) (.add "/websocket" (proxy [WebSocketHandler] [] (onOpen [c] (println "opened" c)) (onClose [c] (println "closed" c)) (onMessage [c j] (println c j)))) (.add (StaticFileHandler. ".")) (.start)))

(defproject ws-intro "1.0.0-SNAPSHOT" :main ws-intro.core :description "FIXME: write description" :dependencies [[org.clojure/clojure "1.3.0"] [org.webbitserver/webbit "0.4.3"] [org.clojure/data.json "0.1.2"]])

<html> <body> <h1>WebSocket Demo</h1> <input id="message" type="text"/> <section id="content"></section> <script src="http://www.google.com/jsapi"></script> <script>google.load("jquery", "1.3")</script> <script src="http://jquery-json.googlecode.com/files/jquery.json-2.2.min.js"> </script> <script src="http://jquery-websocket.googlecode.com/files/jquery.websocket-0.0.1.js"> </script> <script> var ws = $.websocket("ws://127.0.0.1:8080/websocket", { events: { upcased: function(e) { $("#content").html(e.message); }}}); $('#message').change(function(){ ws.send('message', {type: "downcase", message: $("#message").val()});}); </script> </body> </html>

Is your page not loading? =(

What URL did you use? I've been told http://localhost:8080/ doesn't work as well as http://127.0.0.1:8080/

What browser did you use? Everything works for me in Chrome (version 17.0.963.56)

opened #<NettyWebSocketConnection webbit.netty.NettyWebSocketConnection@8c5488>

#<NettyWebSocketConnection webbit.netty.NettyWebSocketConnection@8c5488> {"type":"message","data":{"type":"downcase","message":"hello"}}

(ns ws-intro.core (:require [clojure.data.json :as json] [clojure.string :as s]) (:import [org.webbitserver WebServer WebServers WebSocketHandler] [org.webbitserver.handler StaticFileHandler])) (defn on-message [connection json-message] (let [message (-> json-message json/read-json (get-in [:data :message]))] (.send connection (json/json-str {:type "upcased" :message (s/upper-case message) })))) (defn -main [] (doto (WebServers/createWebServer 8080) (.add "/websocket" (proxy [WebSocketHandler] [] (onOpen [c] (println "opened" c)) (onClose [c] (println "closed" c)) (onMessage [c j] (on-message c j)))) (.add (StaticFileHandler. ".")) (.start)))

:type "upcased"

events

upcased