Code Monkey home page Code Monkey logo

hunchensocket's Introduction

Build Status Hunchensocket - WebSockets for Hunchentoot

Hunchensocket is a Common Lisp implementation of WebSockets realized as an extension to [Edi Weitz'] edi excellent Hunchentoot web server. Hunchensocket implements a compliant RFC6455 server.

Note that Alexander Kahl, the original author, has desactivated his old version that only supports the drafts of the protocol.

Installation

Hunchensocket is in Quicklisp, so if you have that setup just do (ql:quickload :hunchensocket).

Quicklisp is also good to use the trunk alongside with other dependencies, perhaps to test a new feature or a bugfix:

$ cd ~/Source/Lisp/
$ git clone https://github.com/joaotavora/hunchensocket.git
(push "~/Source/Lisp" ql:*local-project-directories*)
(ql:quickload :hunchensocket) ;; use local hunchensocket and pull
                              ;; dependencies from quicklisp

A chat server in 30 lines

First define classes for rooms and users. Make these subclasses of websocket-resource and websocket-client.

(defpackage :my-chat (:use :cl))
(in-package :my-chat)

(defclass chat-room (hunchensocket:websocket-resource)
  ((name :initarg :name :initform (error "Name this room!") :reader name))
  (:default-initargs :client-class 'user))

(defclass user (hunchensocket:websocket-client)
  ((name :initarg :user-agent :reader name :initform (error "Name this user!"))))

Define a list of rooms. Notice that hunchensocket:*websocket-dispatch-table* works just like hunchentoot:*dispatch-table*, but for websocket specific resources.

(defvar *chat-rooms* (list (make-instance 'chat-room :name "/bongo")
                           (make-instance 'chat-room :name "/fury")))

(defun find-room (request)
  (find (hunchentoot:script-name request) *chat-rooms* :test #'string= :key #'name))

(pushnew 'find-room hunchensocket:*websocket-dispatch-table*)

OK, now a helper function and the dynamics of a chat room.

(defun broadcast (room message &rest args)
  (loop for peer in (hunchensocket:clients room)
        do (hunchensocket:send-text-message peer (apply #'format nil message args))))

(defmethod hunchensocket:client-connected ((room chat-room) user)
  (broadcast room "~a has joined ~a" (name user) (name room)))

(defmethod hunchensocket:client-disconnected ((room chat-room) user)
  (broadcast room "~a has left ~a" (name user) (name room)))

(defmethod hunchensocket:text-message-received ((room chat-room) user message)
  (broadcast room "~a says ~a" (name user) message))  

Finally, start the server. hunchensocket:websocket-acceptor works just like hunchentoot:acceptor, and you can probably also use hunchensocket:websocket-ssl-acceptor.

(defvar *server* (make-instance 'hunchensocket:websocket-acceptor :port 12345))
(hunchentoot:start *server*)

Now open two browser windows on http://www.websocket.org/echo.html, enter ws://localhost:12345/bongo as the host and play around chatting with yourself.

License

See COPYING for license details.

Design

Main sources of inspiration:

  • Original implementation by Alexander Kahl, which cleverly hijacks the Hunchentoot connection after the HTTP response and keeps the connection alive, just like in a Head request.
  • clws's API because it explicitly defines websocket "resources"
  • Hunchentoot's's API because it uses CLOS

hunchensocket's People

Contributors

carrotflakes avatar deadtrickster avatar easye avatar hijarian avatar joaotavora avatar mtstickney avatar outergod avatar

Watchers

 avatar  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.