PHP速学视频免费教程(入门到精通)
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
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 <p>它可以运行,但我不知道它在哪个端口上运行,或者一般如何通过客户端调用它。有人有什么想法吗?</p> <p>我认为应该是端口4389,但不是那个</p> <p>我正在使用此脚本进行测试:</p> <pre class="brush:php;toolbar:false;">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 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 <p>这意味着您的代码通过标准输入和输出(stdin/stdout)使用 json-rpc,<em>而不是</em>通过网络连接。<br> 当您使用 <code>os.stdin</code> 作为 <a href="https://www.php.cn/link/0bcd25b83e703478c74c9f575039f3f1" rel="nofollow noreferrer"><code>jsonrpc2.newbufferedstream</code></a> 的参数时,您指定输入应来自运行服务器的进程的标准输入。并且响应将被发送到标准输出。</p> <p>因此,服务器没有侦听任何网络端口。它与直接发送到其标准输入和输出的数据进行交互。这通常用于进程间通信,例如,当您希望一个进程调用服务器进程并接收响应时。<br> 例如,请参阅“<a href="https://www.php.cn/link/7d787bb950167c81630d2f1256eec848">go:与另一个进程进行双向通信?</a>”或<a href="https://www.php.cn/link/8c3c27ac7d298331a1bdfd0a5e8703d3" rel="nofollow noreferrer"><code>davidelorenzoli/stdin-stdout-ipc</code></a>。</p> <p>如果您希望 json-rpc 服务器侦听网络端口,则需要使用 <a href="https://www.php.cn/link/977f8b33d303564416bf9f4ab1c39720" rel="在" go noreferrer><code>net</code> 包</a>。您还需要修改客户端脚本以将其请求发送到正确的网络端口,而不是向 url 发送 http 请求。</p> <pre class="brush:golang;toolbar:false;">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) <p>这是一个基本示例,其中 <code>serve</code> 方法创建一个 tcp 侦听器,侦听本地主机的端口 4389。然后它进入一个等待连接的循环,当它获得连接时,它会启动一个新的 goroutine 来使用 json-rpc 服务器处理该连接。</p> <p>在客户端,您需要打开到服务器的 tcp 连接,将 json-rpc 请求写入该连接,然后读取响应。</p> <p>您不能像在 python 脚本中那样使用 <a href="https://www.php.cn/link/42e499e591496cb71d9a11ae109dc962" rel="nofollow noreferrer"><code>requests</code> 库</a>,因为它用于 http 请求,不是原始 tcp 连接。<br> 您将需要在 python 中使用 <a href="https://www.php.cn/link/b13b768ca3df456b8a0423880c3b239b" rel="nofollow noreferrer"><code>socket</code> 库</a>,或在您客户端的语言,以创建 tcp 连接并通过它发送/接收数据。</p> <p>但请记住,<a href="https://www.php.cn/link/f9e70773c750b0b4ef0b7b2a0b07fe67" rel="nofollow noreferrer">lsp(语言服务器协议)</a> 通过 stdin/stdout 运行而不是网络套接字。<br> 这是因为 lsp 服务器通常由编辑器/ide 作为子进程启动,并通过这些通道直接通信。因此,根据您的用例,原始的 stdin/stdout 方法可能更合适。</p>
golang免费学习笔记(深入):立即学习
在学习笔记中,你将探索golang的核心概念和高级技巧!
已抢7215个
抢已抢94862个
抢已抢14828个
抢已抢52097个
抢已抢194768个
抢已抢87281个
抢