mirror of
https://github.com/sourcegraph/jsonrpc2.git
synced 2026-06-18 21:20:02 +02:00
Add pluggable transport interface + WebSocket support
simplify API by using interface{}, rename from transport -> stream
add WebSocket transport in websocket subpackage
do not buffer in ReadObject, rename GetObjectReader/ReadObject -> NextObjectReader
use xtest (jsonrpc2_test) package to allow us to test subpackages that depend on us (in a future change)
factor out vscode-specific transport code and allow pluggable transports
remove Server (unused) and Serve (unnecessary):
The Serve func had nothing specific to JSON-RPC; it was just a loop
around (net.Listener).Accept. It added no value.
This commit is contained in:
parent
9fdd802ab4
commit
6e06d561ec
6 changed files with 416 additions and 194 deletions
44
websocket/stream.go
Normal file
44
websocket/stream.go
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
// Package websocket provides WebSocket transport support for JSON-RPC
|
||||
// 2.0.
|
||||
package websocket
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
// A ObjectStream is a jsonrpc2.ObjectStream that uses a WebSocket to
|
||||
// send and receive JSON-RPC 2.0 objects.
|
||||
type ObjectStream struct {
|
||||
conn *websocket.Conn
|
||||
}
|
||||
|
||||
// NewObjectStream creates a new jsonrpc2.ObjectStream for sending and
|
||||
// receiving JSON-RPC 2.0 objects over a WebSocket.
|
||||
func NewObjectStream(conn *websocket.Conn) ObjectStream {
|
||||
return ObjectStream{conn: conn}
|
||||
}
|
||||
|
||||
// WriteObject implements jsonrpc2.ObjectStream.
|
||||
func (t ObjectStream) WriteObject(obj interface{}) error {
|
||||
return t.conn.WriteJSON(obj)
|
||||
}
|
||||
|
||||
// ReadObject implements jsonrpc2.ObjectStream.
|
||||
func (t ObjectStream) ReadObject(v interface{}) error {
|
||||
err := t.conn.ReadJSON(v)
|
||||
if e, ok := err.(*websocket.CloseError); ok {
|
||||
if e.Code == websocket.CloseAbnormalClosure && e.Text == io.ErrUnexpectedEOF.Error() {
|
||||
// Suppress a noisy (but harmless) log message by
|
||||
// unwrapping this error.
|
||||
err = io.ErrUnexpectedEOF
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// Close implements jsonrpc2.ObjectStream.
|
||||
func (t ObjectStream) Close() error {
|
||||
return t.conn.Close()
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue