[英]SSE Server in Common Lisp
我试图用通用Lisp编写一个简单的异步服务器。 强调简单。 这是Take 2 (感谢Rainer的建议和格式化) :
(ql:quickload (list :cl-ppcre :usocket))
(defpackage :test-server (:use :cl :cl-ppcre :usocket))
(in-package :test-server)
(defvar *socket-handle* nil)
(defparameter *channel* nil)
(defclass buffer ()
((contents :accessor contents :initform nil)
(started :reader started :initform (get-universal-time))
(state :accessor state :initform :empty)))
(defun listen-on (port &optional (stream *standard-output*))
(setf *socket-handle* (socket-listen "127.0.0.1" port :reuse-address t))
(let ((conns (list *socket-handle*))
(buffers (make-hash-table)))
(loop (loop for ready in (wait-for-input conns :ready-only t)
do (if (typep ready 'stream-server-usocket)
(push (socket-accept ready) conns)
(let ((buf (gethash ready buffers (make-instance 'buffer))))
(buffered-read! (socket-stream ready) buf)
(when (starts-with? (list #\newline #\return #\newline #\return)
(contents buf))
(format stream "COMPLETE ~s~%"
(coerce (reverse (contents buf)) 'string))
(setf conns (remove ready conns))
(remhash ready buffers)
(let ((parsed (parse buf)))
(format stream "PARSED: ~s~%" parsed)
(handle-request ready (parse buf))))))))))
(defmethod parse ((buf buffer))
(let ((lines (split "\\r?\\n" (coerce (reverse (contents buf)) 'string))))
(second (split " " (first lines)))))
HTTP编写:
(defmethod http-write (stream (line-end (eql :crlf)))
(declare (ignore line-end))
(write-char #\return stream)
(write-char #\linefeed stream)
(values))
(defmethod http-write (stream (line string))
(write-string line stream)
(http-write stream :crlf)
(values))
(defmethod http-write (stream (lst list))
(mapc (lambda (thing) (http-write stream thing)) lst)
(values))
如何处理要求:
(defmethod handle-request (socket request)
(let ((s (socket-stream socket)))
(cond ((string= "/sub" request)
(subscribe! socket))
((string= "/pub" request)
(publish! "Got a message!")
(http-write s (list "HTTP/1.1 200 OK"
"Content-Type: text/plain; charset=UTF-8"
"Cache-Control: no-cache, no-store, must-revalidate"
"Content-Length: 10" :crlf
"Published!" :crlf))
(socket-close socket))
(t (http-write s (list "HTTP/1.1 200 OK"
"Content-Type: text/plain; charset=UTF-9"
"Content-Length: 2" :crlf
"Ok" :crlf))
(socket-close socket)))))
发布!
(defun publish! (msg)
(loop for sock in *channel*
do (handler-case
(let ((s (socket-stream sock)))
(format s "data: ~a" msg)
(http-write s (list :crlf :crlf))
(force-output s))
(error (e)
(declare (ignore e))
(setf *channel* (remove sock *channel*))))))
订阅!
(defun subscribe! (sock)
(let ((s (socket-stream sock)))
(http-write s (list "HTTP/1.1 200 OK"
"Content-Type: text/event-stream; charset=utf-8"
"Transfer-Encoding: chunked"
"Connection: keep-alive"
"Expires: Thu, 01 Jan 1970 00:00:01 GMT"
"Cache-Control: no-cache, no-store, must-revalidate" :crlf))
(force-output s)
(push sock *channel*)))
基本实用程序:
(defmethod starts-with? ((prefix list) (list list) &optional (test #'eql))
(loop for (p . rest-p) on prefix for (l . rest-l) on list
when (or (and rest-p (not rest-l)) (not (funcall test p l)))
do (return nil)
finally (return t)))
(defun stop ()
(when *socket-handle*
(loop while (socket-close *socket-handle*))
(setf *socket-handle* nil
*channel* nil)))
(defmethod buffered-read! (stream (buffer buffer))
(loop for char = (read-char-no-hang stream nil :eof)
until (or (null char) (eql :eof char))
do (push char (contents buffer))))
摘要是:
"/sub"
的请求,则应该保留该套接字以进行进一步的写操作。 "/pub"
的请求,则应该向所有现有订阅者发送一条短消息 plain-text
"Ok"
。 与往常一样,欢迎所有反馈。 从版本2开始(添加了HTTP友好的行尾以及策略性地放置了两个force-output
调用) ,浏览器对我来说似乎更快乐,但是当实际将消息发送到现有频道时,Chrome仍然令人窒息。 知道剩下的错误还有多少publish!
是?
明确地说,
var src = new EventSource("/sub");
src.onerror = function (e) { console.log("ERROR", e); };
src.onopen = function (e) { console.log("OPEN", e); };
src.onmessage = function (e) { console.log("MESSAGE", e) };
现在,我在FireFox中获得了一个工作事件流(它触发onopen
,并且onmessage
发送更新都会触发onmessage
) 。 但是在Chrome中失败(触发onopen
,每次更新触发onerror
而不是onmessage
) 。
任何帮助表示赞赏。
我要确保的一件事:它应该在输入和输出上正确处理CRLF。 CRLF在HTTP中使用。
有两个Common Lisp字符: #\\return
和#\\linefeed
。
不要使用#\\newline
。 这是一个特殊字符,取决于操作系统和特定的CL实现。 在Unix操作系统上,它可能与#\\linefeed
相同。 在Windows的实现可能是相同的符和换行符的序列。 因此也不要将换行符用作格式指令~%
。
始终在HTTP协议的末尾显式写入return和换行符。 因此,您可以确保代码具有可移植性,并且可以做正确的事情。
另外,请注意,请确保未使用EQ
完成字符比较。 字符不一定是eq。 使用EQL
比较身份,数字和字符。
好的,所以在尝试了一堆东西之后,我已经开始工作了,但是我不知道为什么。 这将是我的下一个问题。
什么不工作:
force-output
调用的位置/状态(除非同时force-output
执行subscribe!
和publish!
消息,否则根本不会在客户端触发任何事件) babel
将SSE事件编码为八位字节(此操作失败; socket-stream
不是binary-stream
cl-async
重写服务器,该服务器具有自己的写入例程。 可以在这里看到这种努力的结果 ,但是完全没有帮助。 Firefox / Iceweasel / Conkeror的运行正常,但是Chrom (?:e|ium)
仍然失败。 也就是说,事件流正常打开,会触发onopen
事件,但是每发送一个实际事件,就会触发onerror
而不是onmessage
bom
。 在启动流之前执行(write-char (code-char #xfeff) s)
无效。 FF等仍将接受该流,而Safari引擎浏览器仍将拒绝该流。 此时剩下的唯一事情就是清除数据包嗅探器。 使用sniffit
,我发现nginx PushStream模块发出的内容与我的实现发出的内容实际上是有区别的。
我的(是的,我假装为nginx/1.2.0
只是为了最大程度地减少响应之间的差异):
HTTP/1.1 200 OK
Server: nginx/1.2.0
Date: Sun, 15 Oct 2013 10:29:38 GMT-5
Content-Type: text/event-stream; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Expires: Thu, 01 Jan 1970 00:00:01 GMT
Cache-Control: no-cache, no-store, must-revalidate
data: message goes here
Nginx推送流模块:
HTTP/1.1 200 OK
Server: nginx/1.2.0
Date: Sun, 15 Sep 2013 14:40:12 GMT
Content-Type: text/event-stream; charset=utf-8
Connection: close
Expires: Thu, 01 Jan 1970 00:00:01 GMT
Cache-Control: no-cache, no-store, must-revalidate
Transfer-Encoding: chunked
6d
data: message goes here
在我的实现中添加“ 6d”行使其可以正常工作。 我不知道为什么,除非这是我不熟悉的UTF-8中bom
的约定。 换句话说,重写subscribe!
如
(defun subscribe! (sock)
(let ((s (socket-stream sock)))
(http-write s (list "HTTP/1.1 200 OK"
"Content-Type: text/event-stream; charset=utf-8"
"Transfer-Encoding: chunked"
"Connection: keep-alive"
"Expires: Thu, 01 Jan 1970 00:00:01 GMT"
"Cache-Control: no-cache, no-store, must-revalidate" :crlf
"6d"))
(force-output s)
(push sock *channel*)))
绝招。 Chrom (?:e|ium)
现在可以正确接受这些事件流,并且在消息发送时不会出错。
现在我需要确切地了解那里发生了什么...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.