首页 > 后端开发 > Golang > 正文

Golang jsonrpc2 服务器在哪里监听?

王林
发布: 2024-02-08 21:27:28
转载
739 人浏览过

Golang jsonrpc2 服务器在哪里监听?

Golang jsonrpc2 服务器在哪里监听?这是许多Golang开发者在使用jsonrpc2协议时经常遇到的问题。在Golang中,jsonrpc2服务器监听的位置取决于代码的实现方式。一种常见的做法是将服务器监听在特定的端口上,以便接收来自客户端的请求。另外,也可以将服务器监听在特定的网络接口上,例如本地回环接口(localhost)或指定的IP地址上。为了确保服务器能够正常监听请求并进行处理,开发者需要在代码中明确指定监听的位置。

问题内容

我想在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': '[email&#160;protected]','Subject': 'Hello', 'Content': 'Hi!!!'}
smsArgs = {'Number': '381641234567', 'Content': 'Sms!!!'}
print(rpc_call(url, 'email.SendEmail', emailArgs))
print(rpc_call(url, 'sms.SendSMS', smsArgs))
登录后复制

我认为这是正确的,因为我从另一个 stackoverflow 问题中获取了这个客户

解决方法

我明白了:

handlerwitherror(s.handle)

    // create a new jsonrpc2 stream server
    <-jsonrpc2.newconn(
        context.background(),
        jsonrpc2.newbufferedstream(os.stdin, jsonrpc2.vscodeobjectcodec{}),
        handler).disconnectnotify()
}
登录后复制

这意味着您的代码通过标准输入和输出(stdin/stdout)使用 json-rpc,而不是通过网络连接。
当您使用 os.stdin 作为 jsonrpc2.newbufferedstream 的参数时,您指定输入应来自运行服务器的进程的标准输入。并且响应将被发送到标准输出。

因此,服务器没有侦听任何网络端口。它与直接发送到其标准输入和输出的数据进行交互。这通常用于进程间通信,例如,当您希望一个进程调用服务器进程并接收响应时。
例如,请参阅“go:与另一个进程进行双向通信?”或davidelorenzoli/stdin-stdout-ipc

如果您希望 json-rpc 服务器侦听网络端口,则需要使用 net。您还需要修改客户端脚本以将其请求发送到正确的网络端口,而不是向 url 发送 http 请求。

package main

import (
    "context"
    "net"
    "log"
    "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 {
    // Initialize here if needed
    return nil
}

func (s *LSPServer) Handle(context.Context, *jsonrpc2.Conn, *jsonrpc2.Request) (result interface{}, err error) {
    fmt.Println("Handling request...")
    // Handle something
    return nil, nil
}

func (s *LSPServer) Serve(ctx context.Context) {
    fmt.Println("Starting LSP server...")
    
    // Listen on TCP port 4389 on all available unicast and
    // anycast IP addresses of the local system.
    l, err := net.Listen("tcp", "localhost:4389")
    if err != nil {
        log.Fatal(err)
    }
    defer l.Close()

    for {
        // Wait for a connection.
        conn, err := l.Accept()
        if err != nil {
            log.Fatal(err)
        }

        // Handle the connection in a new goroutine.
        go func(c net.Conn) {
            // Create a new jsonrpc2 stream server
            handler := jsonrpc2.HandlerWithError(s.Handle)
            <-jsonrpc2.NewConn(
                ctx,
                jsonrpc2.NewBufferedStream(c, jsonrpc2.VSCodeObjectCodec{}),
                handler).DisconnectNotify()
            c.Close()
        }(conn)
    }
}

func main() {
    // Create a new LSP server
    server := NewLSPServer()
    go server.Serve(context.Background()) // run Serve in a separate goroutine
    select {} // wait forever
}
登录后复制

这是一个基本示例,其中 serve 方法创建一个 tcp 侦听器,侦听本地主机的端口 4389。然后它进入一个等待连接的循环,当它获得连接时,它会启动一个新的 goroutine 来使用 json-rpc 服务器处理该连接。

在客户端,您需要打开到服务器的 tcp 连接,将 json-rpc 请求写入该连接,然后读取响应。

您不能像在 python 脚本中那样使用 requests,因为它用于 http 请求,不是原始 tcp 连接。
您将需要在 python 中使用 socket,或在您客户端的语言,以创建 tcp 连接并通过它发送/接收数据。

但请记住,lsp(语言服务器协议) 通过 stdin/stdout 运行而不是网络套接字。
这是因为 lsp 服务器通常由编辑器/ide 作为子进程启动,并通过这些通道直接通信。因此,根据您的用例,原始的 stdin/stdout 方法可能更合适。

以上是Golang jsonrpc2 服务器在哪里监听?的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:stackoverflow.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!