Working Clojure Netty client code.
This commit is contained in:
parent
9aa2844856
commit
297a818066
|
@ -5,8 +5,7 @@
|
|||
[org.eclipse.jetty/jetty-websocket "7.5.4.v20111024"]
|
||||
[org.eclipse.jetty/jetty-server "7.5.4.v20111024"]
|
||||
[org.eclipse.jetty/jetty-servlet "7.5.4.v20111024"]
|
||||
;[commons-codec/commons-codec "1.4"]
|
||||
;[clj-base64 "0.0.0-SNAPSHOT"]
|
||||
[org.jboss.netty/netty "3.2.5.Final"]
|
||||
]
|
||||
; :dev-dependencies [[swank-clojure "1.3.0-SNAPSHOT"]]
|
||||
)
|
||||
|
|
|
@ -2,7 +2,20 @@
|
|||
(:use ring.adapter.jetty)
|
||||
;(:import [org.jboss.netty.handler.codec.base64 Base64])
|
||||
|
||||
(:import [java.net URL URI]
|
||||
|
||||
(:import
|
||||
;[java.io BufferedReader DataOutputStream]
|
||||
|
||||
[java.net InetSocketAddress]
|
||||
[java.nio ByteBuffer]
|
||||
[java.nio.channels SocketChannel]
|
||||
|
||||
[org.jboss.netty.channel
|
||||
Channels SimpleChannelHandler ChannelPipelineFactory]
|
||||
[org.jboss.netty.channel.socket.nio NioClientSocketChannelFactory]
|
||||
[org.jboss.netty.bootstrap ClientBootstrap]
|
||||
[java.util.concurrent Executors]
|
||||
|
||||
[org.eclipse.jetty.server Server]
|
||||
[org.eclipse.jetty.server.nio BlockingChannelConnector]
|
||||
[org.eclipse.jetty.servlet ServletContextHandler ServletHolder DefaultServlet]
|
||||
|
@ -12,22 +25,68 @@
|
|||
|
||||
(defonce settings (atom {}))
|
||||
|
||||
;; TCP / NIO
|
||||
|
||||
;; (defn tcp-channel [host port]
|
||||
;; (try
|
||||
;; (let [address (InetSocketAddress. host port)
|
||||
;; channel (doto (SocketChannel/open)
|
||||
;; (.connect address))]
|
||||
;; channel)
|
||||
;; (catch Exception e
|
||||
;; (println (str "Failed to connect to'" host ":" port "':" e))
|
||||
;; nil)))
|
||||
|
||||
;; http://docs.jboss.org/netty/3.2/guide/html/start.html#d0e51
|
||||
(defn make-netty-client-handler []
|
||||
(proxy [SimpleChannelHandler] []
|
||||
(channelConnected [ctx e]
|
||||
(println "channelConnected:" e))
|
||||
(channelDisconnected [ctx e]
|
||||
(println "channelDisconnected:" e))
|
||||
(messageReceived [ctx e]
|
||||
(println "messageReceived:" (.getMessage e)))
|
||||
(exceptionCaught [ctx e]
|
||||
(println "exceptionCaught:" e))))
|
||||
|
||||
(defn netty-client [host port]
|
||||
(let [pipeline (proxy [ChannelPipelineFactory] []
|
||||
(getPipeline []
|
||||
(doto (Channels/pipeline)
|
||||
(.addLast "handler" (make-netty-client-handler)))))
|
||||
bootstrap (doto (ClientBootstrap.
|
||||
(NioClientSocketChannelFactory.
|
||||
(Executors/newCachedThreadPool)
|
||||
(Executors/newCachedThreadPool)))
|
||||
(.setPipelineFactory pipeline)
|
||||
(.setOption "tcpNoDelay" true)
|
||||
(.setOption "keepAlive" true))
|
||||
channel-future (.connect bootstrap (InetSocketAddress. host port))
|
||||
channel (.. channel-future (awaitUninterruptibly) (getChannel))]
|
||||
channel))
|
||||
|
||||
|
||||
|
||||
;; WebSockets
|
||||
|
||||
|
||||
(defonce clients (atom {}))
|
||||
|
||||
|
||||
;; http://wiki.eclipse.org/Jetty/Feature/WebSockets
|
||||
(defn make-websocket-handler []
|
||||
(reify org.eclipse.jetty.websocket.WebSocket$OnTextMessage
|
||||
(onOpen [this connection]
|
||||
(println "Got WebSocket connection:" connection)
|
||||
(swap! clients assoc this connection))
|
||||
#_(let [target (tcp-channel "localhost" 5901)]
|
||||
(swap! clients assoc this {:client connection
|
||||
:target target})))
|
||||
(onClose [this code message]
|
||||
(println "Got WebSocket close:" code message)
|
||||
(swap! clients dissoc this))
|
||||
(onMessage [this data]
|
||||
(println "Got WebSocket message:" data))))
|
||||
|
||||
(defn websocket-servlet []
|
||||
(defn make-websocket-servlet []
|
||||
(proxy [org.eclipse.jetty.websocket.WebSocketServlet] []
|
||||
(doGet [request response]
|
||||
;(println "doGet" request)
|
||||
|
@ -47,7 +106,7 @@
|
|||
(let [http-servlet (doto (ServletHolder. (DefaultServlet.))
|
||||
(.setInitParameter "dirAllowed" "true")
|
||||
(.setInitParameter "resourceBase" web))
|
||||
ws-servlet (ServletHolder. (websocket-servlet))
|
||||
ws-servlet (ServletHolder. (make-websocket-servlet))
|
||||
context (doto (ServletContextHandler.)
|
||||
(.setContextPath "/")
|
||||
(.addServlet ws-servlet "/websocket"))
|
||||
|
|
Loading…
Reference in New Issue