• Stars
    star
    109
  • Rank 319,077 (Top 7 %)
  • Language
    Common Lisp
  • License
    Other
  • Created over 10 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

RFC6455 compliant WebSockets for Common Lisp

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

More Repositories

1

yasnippet

A template system for Emacs
Emacs Lisp
2,770
star
2

eglot

A client for Language Server Protocol servers
Emacs Lisp
2,256
star
3

sly

Sylvester the Cat's Common Lisp IDE
Common Lisp
1,256
star
4

autopair

Automagically pair braces and quotes in emacs like TextMate
Emacs Lisp
208
star
5

snooze

Common Lisp RESTful web development
Common Lisp
206
star
6

breadcrumb

Emacs headerline indication of where you are in a large project
Emacs Lisp
189
star
7

darkroom

Simple distraction-free editing
Emacs Lisp
147
star
8

beardbolt

Compiler Explorer clone
Emacs Lisp
86
star
9

fiasco

A test framework for Common Lisp
Common Lisp
55
star
10

snippet

A rewrite of Yasnippet's engine (incomplete, but many parts done)
Emacs Lisp
30
star
11

sly-quicklisp

Quicklisp support for SLY
Emacs Lisp
29
star
12

sly-macrostep

Expand CL macros inside source files
Emacs Lisp
28
star
13

sly-stepper

sly-slepper
TeX
27
star
14

yasmate

Convert textmate bundles to yasnippet format
Emacs Lisp
24
star
15

eslack

Slack client for Emacs
Emacs Lisp
12
star
16

sly-hello-world

A dummy template for writing SLY contribs
Emacs Lisp
9
star
17

sly-named-readtables

NAMED-READTABLES support for SLY
Emacs Lisp
9
star
18

zapp

Zebugger Adapter Protocol Plugin
Emacs Lisp
8
star
19

emacs-livereload

A livereload server running on Emacs
JavaScript
7
star
20

mac-key-mode

Provide mac-style key bindings on Carbon Emacs
Emacs Lisp
6
star
21

sinatra-thumbnails

Dynamic generation, serving and caching of thumbnail images for simple file-based CMS's written in Sinatra.
Ruby
6
star
22

efire

A campfire client for emacs
Emacs Lisp
5
star
23

nestor

A common-lisp clone of the nesta ruby cms
Common Lisp
5
star
24

cl-guard

A uniform interface to your file and event watching needs in common lisp.
Common Lisp
4
star
25

eel

Emacs interface to voidtools Everything file search
Emacs Lisp
4
star
26

ecco

ecco is a port of docco for emacs
Emacs Lisp
4
star
27

elisp-shorthand

Pretty dumb namespacing system
Emacs Lisp
3
star
28

hello-world-2000

Simplest possible C++ Hello World with CMake+Conan
CMake
3
star
29

tcpppl4

Exercise solutions for The C++ Programming Language 4th edition
C++
2
star
30

st

My fork of simple terminal
C
2
star
31

treeconf

C++ command-line parser for tree-like options
C++
2
star
32

blip

Automatically find and run tests in emacs
Emacs Lisp
2
star
33

lsplex

Experimental Language Server proxy/multiplexer
C++
2
star
34

whopper

A bare bones fork of http://common-lisp.net/project/bese/yaclml.html
Common Lisp
2
star
35

edice

Why would you want to roll dice in Emacs?
Emacs Lisp
2
star
36

vulkan-man-pages

Archlinux package for vulkan man pages
Shell
1
star
37

minns

a minimal name server, written as an exercise in C++
C++
1
star
38

revbufs

Reverts all out-of-date buffers safely
Emacs Lisp
1
star
39

raytracing-one-weekend

working through https://raytracing.github.io/books/RayTracingInOneWeekend.html
C++
1
star
40

nih

A shrubbery!!!
Emacs Lisp
1
star
41

blinky-template

A bare-bones Makefile-based template for ESP32 projects
C
1
star