Giter VIP home page Giter VIP logo

websocket-server's Introduction

WebSocket Server

This is the server side of the websocket connection. This is meant to be paired with fentontravers/websocket-client.

Clojars

Foo

Usage

(require '[websocket-server.core :refer :all])

(defn request-handler-upcase-string
  "The function that will take incoming data off the websocket,
  process it and return a reponse.  In our case we'll simply UPPERCASE
  whatever is received."
  [data] (clojure.string/upper-case (str data)))

; Always call this module functions with a port, unless you want to apply it to every server you opened on all ports.
(def port 8899)

(defn start
  "Demonstrate how to use the websocket server library."
  []
  (start-ws-server port :on-receive request-handler-upcase-string))

(defn send-all!
  [data]
  (send-all! port data))

(defn stop
  "Stop websocket server"
  []
  (stop-ws-server port))

Here is another example that expects EDN in the form of a map that looks like {:count 1}, or just a map with a key :count and some integer value. Then it increments that value by 10 and returns it back.

(defn request-handler-add10 
  [data]
  (->> data
       edn/read-string
       :count
       (+ 10)
       (hash-map :count)
       str))

Multiple servers usage

(start-ws-server 8000 :on-receive request-handler-1)
(start-ws-server 8001 :on-receive request-handler-2)
(start-ws-server 8002 :on-receive request-handler-3)

; Send "Hello" to all channels opened to websocket on port 8000
(send-all! 8000 "Hello")

(stop-ws-server 8002)

; Send "Hi!" to all channels opened to websocket on port 8000 or 8001
(send-all! "Hello")

(stop-all-ws-servers)

Use transformer functions when receiving and sending data

(require '[clojure.data.json :as json])

(defn request-handler-json
    [[action data]]
    [action
     (case action
        "upcase" (request-handler-upcase-string data)
        data)])

; json/read-str will be applied before sending data to request-json-handler
; json/write-str will be applied before sending data from request-json-handler
;   back on the websocket, or when using send-all!
(start-ws-server 8000
  :on-receive request-handler-json
  :in-fn json/read-str
  :out-fn json/write-str)

websocket-server's People

Contributors

ftravers avatar juleffel avatar paullucas avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.