我想在golang中创建一个简单的LSP服务器,到目前为止,这是我写的代码:
package main
import (
"context"
"fmt"
"os"
"sync"
"github.com/sourcegraph/jsonrpc2"
)
type LSPServer struct {
// The symmetric connection
conn jsonrpc2.Conn
// Check if the connection is available
connMutex sync.Mutex
// shutdown
shutdown bool
}
func NewLSPServer() *LSPServer {
return &LSPServer{}
}
func (s *LSPServer) Initialize(ctx context.Context) error {
// to implement
return nil
}
func (s *LSPServer) Handle(context.Context, *jsonrpc2.Conn, *jsonrpc2.Request) (result interface{}, err error) {
fmt.Println("Handling request...")
// to implement
return nil, nil
}
func (s *LSPServer) Serve(ctx context.Context) {
fmt.Println("Starting LSP server...")
// what port is this server listening on?
// it is listening on port 4389
// Create a new jsonrpc2 stream server
handler := jsonrpc2.HandlerWithError(s.Handle)
// Create a new jsonrpc2 stream server
<-jsonrpc2.NewConn(
context.Background(),
jsonrpc2.NewBufferedStream(os.Stdin, jsonrpc2.VSCodeObjectCodec{}),
handler).DisconnectNotify()
}
func main() {
// Create a new LSP server
server := NewLSPServer()
server.Serve(context.Background())
}
字符串
它可以运行,但我不知道它在哪个端口上运行,也不知道如何用客户端调用它。有人有主意吗?
我想应该是4389端口,但不是那个
我用这个脚本测试:
import json
import requests
def rpc_call(url, method, args):
headers = {'content-type': 'application/json'}
payload = {
"method": method,
"params": [args],
"jsonrpc": "2.0",
"id": 1,
}
response = requests.post(url, data=json.dumps(payload), headers=headers).json()
return response['result']
url = 'http://localhost:4389/'
emailArgs = {'To': 'demo@example.com','Subject': 'Hello', 'Content': 'Hi!!!'}
smsArgs = {'Number': '381641234567', 'Content': 'Sms!!!'}
print(rpc_call(url, 'email.SendEmail', emailArgs))
print(rpc_call(url, 'sms.SendSMS', smsArgs))
型
我认为这是正确的,因为我把这个客户端从另一个stackoverflow问题
2条答案
按热度按时间k5ifujac1#
我看到:
字符串
这意味着你的代码是在标准输入和输出(stdin/stdout)上使用JSON-RPC,而不是在网络连接上。
当您使用
os.Stdin
作为jsonrpc2.NewBufferedStream
的参数时,您指定输入应该来自运行服务器的进程的标准输入。并且响应将被发送到标准输出。因此,服务器不侦听任何网络端口。它与直接发送到其标准输入和输出的数据进行交互。这通常用于进程间通信,例如,当您希望一个进程调用服务器进程并接收响应时。
例如,参见“Go: bidirectional communication with another process?”或
davidelorenzoli/stdin-stdout-ipc
。如果你想让你的JSON-RPC服务器监听一个网络端口,你需要在Go中使用
net
package设置一个网络连接。您还需要修改客户端脚本,以便将其请求发送到正确的网络端口,而不是将HTTP请求发送到URL。型
这是一个基本示例,其中
Serve
方法创建了一个TCP侦听器,该侦听器侦听本地主机的端口4389。然后,它进入一个循环,等待连接,当它得到一个连接时,它启动一个新的goroutine来使用JSON-RPC服务器处理该连接。在客户端,您需要打开到服务器的TCP连接,将JSON-RPC请求写入该连接,然后读取响应。
您不能像在Python脚本中那样使用
requests
library,因为它用于HTTP请求,而不是原始TCP连接。您需要使用Python中的
socket
library或客户端语言中的类似库来创建TCP连接并通过它发送/接收数据。但请记住,LSP (Language Server Protocol)是通过stdin/stdout而不是网络套接字操作的。
这是因为LSP服务器通常由编辑器/IDE作为子进程启动,并通过这些通道直接通信。因此,根据您的用例,原始的stdin/stdout方法可能更合适。
pvcm50d12#
这是对@VonC优秀答案的补充。这个答案提供了一个
stdioReadWriteCloser
来使原始的stdin/stdout方法工作。请先阅读@VonC的回答。在原始问题中构建源代码:
字符串
然后将此消息发送到
lspserver
的stdin
:型
(Note:
Content-Length: %d\r\n\r\n
是VSCodeObjectCodec所必需的)注意错误消息。它尝试写入
stdin
。这是不正确的。我们必须从stdin
和stdout
创建一个io.ReadWriteCloser
,如下所示:型
使用
stdioReadWriteCloser
如下:型
构建
lspserver
并重试:型
现在它正如预期的那样工作!
奇怪的是,
github.com/sourcegraph/jsonrpc2
没有提供这样一个开箱即用的 Package 器。