add websocket example
This commit is contained in:
3 files changed
+85
No files matched your search
@@ -0,0 +1,35 @@
|
|||||||
|
;; -*- coding: utf-8; lexical-binding: t; -*-
|
||||||
|
|
||||||
|
(require 'websocket)
|
||||||
|
|
||||||
|
(setq-local websocket-eldoc-callback nil)
|
||||||
|
(defun test-websocket-eldoc (callback)
|
||||||
|
(setq websocket-eldoc-callback
|
||||||
|
(if (compiled-function-p callback) callback nil)))
|
||||||
|
(set (make-local-variable 'eldoc-documentation-strategy)
|
||||||
|
'eldoc-documentation-compose-eagerly)
|
||||||
|
|
||||||
|
(run-with-timer 1 nil
|
||||||
|
(lambda ()
|
||||||
|
(funcall websocket-eldoc-callback "hel")))
|
||||||
|
|
||||||
|
(test-websocket-eldoc "hel")
|
||||||
|
|
||||||
|
(make-local-variable 'eldoc-documentation-functions)
|
||||||
|
(add-to-list 'eldoc-documentation-functions #'test-websocket-eldoc)
|
||||||
|
|
||||||
|
(setq ws-server (websocket-server
|
||||||
|
4000
|
||||||
|
:host 'local
|
||||||
|
:on-message (lambda (ws frame)
|
||||||
|
(when (and (boundp 'websocket-eldoc-callback) (compiled-function-p websocket-eldoc-callback))
|
||||||
|
(websocket-send-text ws (format "eldoc showed: %s" (websocket-frame-text frame)))
|
||||||
|
(funcall websocket-eldoc-callback (websocket-frame-text frame))))))
|
||||||
|
|
||||||
|
(funcall (symbol-value (obarray-get obarray "websocket-eldoc-callback")) "hello")
|
||||||
|
|
||||||
|
(symbol-value 'websocket-eldoc-callback)
|
||||||
|
|
||||||
|
(websocket-server-close ws-server)
|
||||||
|
|
||||||
|
(funcall websocket-eldoc-callback "hello")
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
<!doctype html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8" />
|
||||||
|
<title>emacs websocket test</title>
|
||||||
|
<script type="text/javascript" src="./websocket.js"></script>
|
||||||
|
</head>
|
||||||
|
<body></body>
|
||||||
|
</html>
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
let ws = new WebSocket("ws://127.0.0.1:4000");
|
||||||
|
ws.onopen = () => {
|
||||||
|
console.log('websocket opened');
|
||||||
|
}
|
||||||
|
|
||||||
|
ws.onclose = () => {
|
||||||
|
console.log('websocket closed');
|
||||||
|
}
|
||||||
|
|
||||||
|
ws.onmessage = (e) => {
|
||||||
|
console.log('recv:', e.data)
|
||||||
|
}
|
||||||
|
|
||||||
|
let timeout = 0;
|
||||||
|
let timer = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送消息到 Emacs
|
||||||
|
* @param {string} msg 消息内容
|
||||||
|
*/
|
||||||
|
function sendMsgToEmacs(msg) {
|
||||||
|
if (!(ws instanceof WebSocket) || ws.readyState !== WebSocket.OPEN) return;
|
||||||
|
|
||||||
|
ws.send(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
const range = 10;
|
||||||
|
|
||||||
|
function calcNextTimeout() {
|
||||||
|
return (Math.round(Math.random() * range) + 2) * 1000;
|
||||||
|
}
|
||||||
|
|
||||||
|
function schedule() {
|
||||||
|
sendMsgToEmacs(window.crypto.randomUUID());
|
||||||
|
window.clearTimeout(timer);
|
||||||
|
timeout = calcNextTimeout();
|
||||||
|
|
||||||
|
timer = window.setTimeout(schedule, timeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
schedule();
|
||||||
Reference in new issue
Block a user