【求助】Emacs lisp 中 Jsonrpc 的使用问题

最初在Emacs-general中发送。
colawithsauce

话说jsonrpc有没有什么使用例?

看官方的那个文档没有看懂

(defclass jsonrpc-connection ()
  ((name
    :accessor jsonrpc-name
    :initarg :name
    :documentation "A name for the connection")
   (-request-dispatcher
    :accessor jsonrpc--request-dispatcher
    :initform #'ignore
    :initarg :request-dispatcher
    :documentation "Dispatcher for remotely invoked requests.")
   (-notification-dispatcher
    :accessor jsonrpc--notification-dispatcher
    :initform #'ignore
    :initarg :notification-dispatcher
    :documentation "Dispatcher for remotely invoked notifications.")
   (last-error
    :accessor jsonrpc-last-error
    :documentation "Last JSONRPC error message received from endpoint.")
   (-request-continuations
    :initform (make-hash-table)
    :accessor jsonrpc--request-continuations
    :documentation "A hash table of request ID to continuation lambdas.")
   (-events-buffer
    :accessor jsonrpc--events-buffer
    :documentation "A buffer pretty-printing the JSONRPC events")
   (-events-buffer-scrollback-size
    :initarg :events-buffer-scrollback-size
    :accessor jsonrpc--events-buffer-scrollback-size
    :documentation "Max size of events buffer.  0 disables, nil means infinite.")
   (-deferred-actions
    :initform (make-hash-table :test #'equal)
    :accessor jsonrpc--deferred-actions
    :documentation "Map (DEFERRED BUF) to (FN TIMER ID).  FN is\
a saved DEFERRED `async-request' from BUF, to be sent not later\
than TIMER as ID.")
   (-next-request-id
    :initform 0
    :accessor jsonrpc--next-request-id
    :documentation "Next number used for a request"))
  :documentation "Base class representing a JSONRPC connection.
The following initargs are accepted:

:NAME (mandatory), a string naming the connection

:REQUEST-DISPATCHER (optional), a function of three
arguments (CONN METHOD PARAMS) for handling JSONRPC requests.
CONN is a `jsonrpc-connection' object, method is a symbol, and
PARAMS is a plist representing a JSON object.  The function is
expected to return a JSONRPC result, a plist of (:result
RESULT) or signal an error of type `jsonrpc-error'.

:NOTIFICATION-DISPATCHER (optional), a function of three
arguments (CONN METHOD PARAMS) for handling JSONRPC
notifications.  CONN, METHOD and PARAMS are the same as in
:REQUEST-DISPATCHER.")

比如这个jsonrpc-connection的类,我如果想让他连接 localhost:8970,应该怎么实例化这个类呢?

(jsonrpc-connection-send CONN &key ID METHOD PARAMS RESULT ERROR)

Documentation
Send a JSONRPC message to connection CONN.

ID, METHOD, PARAMS, RESULT and ERROR.
```

也不是在send的时候传入服务端位置

安装 elisp-demos,然后 M-x descripte-function jsonrpc-process-connection 查看例子:

jsonrpc-process-connection is a byte-compiled Lisp function in ‘jsonrpc.el’.

(jsonrpc-process-connection &rest SLOTS)

Create a new object of class type ‘jsonrpc-process-connection’.

  This function has a compiler macro
    ‘jsonrpc-process-connection--anon-cmacro’.  See the manual for details.

#+BEGIN_SRC elisp
(make-network-process
 :name "JSONRPC server"
 :server t
 :host "localhost"
 :service 44444
 :log
 (lambda (_server client _message)
   (jsonrpc-process-connection
    :name (process-name client)
    :process client
    :request-dispatcher
    (lambda (_endpoint method params)
      (unless (memq method '(+ - * /))
        (signal 'jsonrpc-error
                '((jsonrpc-error-message . "Sorry, this isn't allowed")
                  (jsonrpc-error-code . -32601))))
      (apply method (append params nil))))))

(jsonrpc-process-connection
 :name "JSONRPC client"
 :process (make-network-process
           :name "JSONRPC client process"
           :host "localhost"
           :service 44444))

(jsonrpc-request
 (process-get
  (get-process "JSONRPC client process")
  'jsonrpc-connection)
 '* [3 4])
#+END_SRC

#+RESULTS:
: 12

[back]