Vscode语言客户端扩展 - 如何从服务器向客户端发送消息? [英] Vscode Language Client extension - how to send a message from the server to the client?

查看:314
本文介绍了Vscode语言客户端扩展 - 如何从服务器向客户端发送消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用语言服务器协议开发一个使用客户端和服务器的vscode扩展。

I have been developing a vscode extension that consits of client and server using the language server protocol.

目前,我正在尝试执行以下操作:当服务器检测到某个条件时,他请求客户端将一定数量的文件加载到工作区中。

At the moment, I am trying to do the following thing: when the server detects a certain condition, he requests the client to load a certain number of files into the workspace.

我遇到了严重的问题。由于语言服务器协议没有执行此操作的特定请求,我考虑从服务器向客户端发送消息,一旦客户端检测到此消息,他将继续执行此命令。

I am having serious problems doing this. Since the language server protocol does not have a specific request to do this I thought about sending a message from the server to the client and once the client detects this message he would proceed to execute this command.

问题是,我也不知道该怎么做。有人可以帮帮我吗?

The problem is, I also do not know how to do this. Can anyone please help me?

推荐答案

只要您确定该名称不会与现有的LSP方法发生冲突,可以定义自己的任意方法。例如,在官方lsp样本中,你可以这样做:

As long as you're sure the name doesn't collide with existing LSP methods, you can define arbitrary methods of your own. For instance, in the official lsp-sample, you could do this:

(在 client / src / extension.ts 的末尾)

let client = new LanguageClient('lspSample', 'Language Server Example', serverOptions, clientOptions);
client.onReady().then(() => {
    client.onNotification("custom/loadFiles", (files: Array<String>) => {
        console.log("loading files " + files);
    });
});
context.subscriptions.push(client.start());

(在 documents.onDidChangeContent 的监听器中 server / src / server.ts

var files = ["path/to/file/a.txt", "path/to/file/b.txt"];
connection.sendNotification("custom/loadFiles", [files]);

每次更改 .txt 文件(因为该示例使用 plaintext 作为其文档选择器):

This will output the following to the dev console whenever you change the contents of a .txt file (since the sample uses plaintext as its document selector):


加载文件路径/到/ file / a.txt,路径/到/ file / b.txt

loading files path/to/file/a.txt,path/to/file/b.txt

在自定义方法的名称,参数或调用它们时,您几乎拥有完全的灵活性。语言服务器使用这样的自定义方法是很常见的,这些方法不是用于各种目的的协议的一部分(高级功能,内部调试/开发功能等......)。

You pretty much have complete flexibility here when it comes to the names of custom methods, their parameters or when you invoke them. It's quite common for language servers to use custom methods like this that are not part of the protocol for various purposes (advanced functionality, internal debugging/development features, etc...).

这篇关于Vscode语言客户端扩展 - 如何从服务器向客户端发送消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆