├── .gitignore ├── README.md ├── project.clj ├── src └── clojure_netty │ └── core.clj └── target └── stale └── extract-native.dependencies /.gitignore: -------------------------------------------------------------------------------- 1 | /pom.xml 2 | *jar 3 | /lib 4 | /classes 5 | /native 6 | /.lein-failures 7 | /checkouts 8 | /.lein-deps-sum 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # clojure-netty 2 | Playing with netty in Clojure. 3 | 4 | ## Usage 5 | lein deps 6 | lein run 7 | 8 | ## Source origin 9 | http://stackoverflow.com/questions/1735776/server-programming-with-clojure 10 | 11 | ## Sending a message 12 | 13 | The default port is 5000. You can telnet to localhost 5000 and send the 14 | server a message which it will print to the server's console (so of course 15 | open up two terminals). If 5000 doesn't work, start the server a second 16 | time (while the first is still running) and you should get an error message 17 | with the port number. 18 | 19 | Example: 20 | 21 | /dev/clojure-netty cvig (master)$ telnet localhost 5000 22 | Trying ::1... 23 | Connected to localhost. 24 | Escape character is '^]'. 25 | StackOverflow is pretty cool 26 | ^] 27 | telnet> quit 28 | Connection closed. 29 | 30 | Server console: 31 | 32 | /dev/clojure-netty cvig (master)$ lein run 33 | # 34 | Connected: # /0:0:0:0:0:0:0:1%0:5000]> 35 | Message: StackOverflow is pretty cool 36 | from # /0:0:0:0:0:0:0:1%0:5000]> 37 | Disconnected: # /0:0:0:0:0:0:0:1%0:5000]> 38 | -------------------------------------------------------------------------------- /project.clj: -------------------------------------------------------------------------------- 1 | (defproject clojure-netty "0.0.1-SNAPSHOT" 2 | :description "Basic example of using netty with Clojure" 3 | :dependencies [[org.clojure/clojure "1.4.0-beta1"] 4 | [io.netty/netty "3.6.3.Final"]] 5 | :main clojure-netty.core) 6 | -------------------------------------------------------------------------------- /src/clojure_netty/core.clj: -------------------------------------------------------------------------------- 1 | (ns clojure-netty.core 2 | (:gen-class) 3 | (:import 4 | [java.net InetSocketAddress] 5 | [java.util.concurrent Executors] 6 | [org.jboss.netty.bootstrap ServerBootstrap] 7 | [org.jboss.netty.channel Channels ChannelPipelineFactory 8 | SimpleChannelHandler] 9 | [org.jboss.netty.channel.socket.nio NioServerSocketChannelFactory] 10 | [org.jboss.netty.buffer ChannelBuffers])) 11 | 12 | (declare make-handler) 13 | 14 | (defn start 15 | "Start a Netty server. Returns the pipeline." 16 | [port handler] 17 | (let [channel-factory (NioServerSocketChannelFactory. 18 | (Executors/newCachedThreadPool) 19 | (Executors/newCachedThreadPool)) 20 | bootstrap (ServerBootstrap. channel-factory) 21 | pipeline (.getPipeline bootstrap)] 22 | (.addLast pipeline "handler" (make-handler)) 23 | (.setOption bootstrap "child.tcpNoDelay", true) 24 | (.setOption bootstrap "child.keepAlive", true) 25 | (.bind bootstrap (InetSocketAddress. port)) 26 | pipeline)) 27 | 28 | (defn make-handler 29 | "Returns a Netty handler." 30 | [] 31 | (proxy [SimpleChannelHandler] [] 32 | (channelConnected [ctx e] 33 | (let [c (.getChannel e)] 34 | (println "Connected:" c))) 35 | 36 | (channelDisconnected [ctx e] 37 | (let [c (.getChannel e)] 38 | (println "Disconnected:" c))) 39 | 40 | (messageReceived [ctx e] 41 | (let [c (.getChannel e) 42 | cb (.getMessage e) 43 | msg (.toString cb "UTF-8")] 44 | (println "Message:" msg "from" c))) 45 | 46 | (exceptionCaught 47 | [ctx e] 48 | (let [throwable (.getCause e)] 49 | (println "@exceptionCaught" throwable)) 50 | (-> e .getChannel .close)))) 51 | 52 | (defn -main [] 53 | (start 5000 make-handler)) 54 | -------------------------------------------------------------------------------- /target/stale/extract-native.dependencies: -------------------------------------------------------------------------------- 1 | ([:dependencies ([org.clojure/clojure "1.4.0-beta1"] [io.netty/netty "3.6.3.Final"])]) --------------------------------------------------------------------------------