Skip to content

Instantly share code, notes, and snippets.

@ryanwersal
Created October 21, 2014 04:18
Show Gist options
  • Save ryanwersal/aa9c54ad583b1cc6dd1b to your computer and use it in GitHub Desktop.
Save ryanwersal/aa9c54ad583b1cc6dd1b to your computer and use it in GitHub Desktop.
msgpack based websocket communication between clojure and python
(ns example.core
(:gen-class)
(:require [immutant.web :as web]
[immutant.web.websocket :as ws]
[ring.middleware.resource :refer [wrap-resource]]
[ring.util.response :refer [redirect]]
[msgpack.core :as msgpack]))
(defn -main
[& args]
(web/run
(-> (fn [{c :context}] (redirect (str c "/index.html")))
(wrap-resource "public")
(ws/wrap-websocket {:on-message (fn [c m]
(->> m msgpack/unpack println))
:on-open (fn [c m]
(do (def channel c)
(ws/send! c (->> "Thanks for connecting!" msgpack/pack))))
:on-close (fn [c m]
(def channel nil))}))
{:path "/ws" :host "127.0.0.1"}))
import msgpack
from ws4py.client.threadedclient import WebSocketClient
from ws4py.messaging import BinaryMessage
class Client(WebSocketClient):
def received_message(self, m):
print msgpack.unpackb(m.data)
def send_message(self, m):
msg = BinaryMessage(msgpack.packb(m))
self.send(msg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment