Skip to content

Instantly share code, notes, and snippets.

@PuercoPop
Created April 24, 2015 17:26
Show Gist options
  • Select an option

  • Save PuercoPop/e502441885bf66ae43cf to your computer and use it in GitHub Desktop.

Select an option

Save PuercoPop/e502441885bf66ae43cf to your computer and use it in GitHub Desktop.
(defpackage :slynk-websocket-proxy
(:use :cl :hunchensocket))
(in-package :slynk-websocket-proxy)
(defclass ws-proxy (websocket-resource)
((slynk-channel :initarg :channel
:initform (make-instance 'slynk::channel)
:reader channel
:documentation "The slynk part")
(%thread :accessor %thread)))
(defmethod client-connected ((proxy ws-proxy) user)
"Spawn a thread to constantly poll the Channel for input and send it to the
ws clients."
#+nil(slynk-mrepl:create-mrepl ...)
(labels ((poll ()
;; Assume only one client.
(send-text-message (car (clients proxy))
(slynk-backend::receive))
(poll)))
(setf (%thread proxy) (slynk-backend:spawn #'poll :name "Slynk Proxy"))))
(defmethod client-disconnected ((proxy ws-proxy) user)
"Close the channel."
(slynk-api:close-channel (channel proxy)))
(defmethod text-message-received ((proxy ws-proxy) user message)
(slynk::send-to-remote-channel (slynk::channel-id (channel proxy)) message))
;; URL localhost:6969/slynk/
(defvar *proxy* (make-instance 'ws-proxy :channel (car (slynk::channels))))
(defun find-proxy (request)
(when (string= "/slynk/" (hunchentoot:script-name request))
*proxy*))
(pushnew 'find-proxy hunchensocket:*websocket-dispatch-table*)
(defvar *server* (make-instance 'websocket-acceptor :port 6969))
(hunchentoot:start *server*)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment