Created
October 21, 2014 04:18
-
-
Save ryanwersal/aa9c54ad583b1cc6dd1b to your computer and use it in GitHub Desktop.
msgpack based websocket communication between clojure and python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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"})) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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