UNPKG

2.63 kBMarkdownView Raw
1# VSCode WebSocket JSON RPC
2
3NPM module to implement communication between a jsonrpc client and server over WebSocket.
4
5See the following example code how to use this library or take a look of the `monaco-languageclient` and `vscode-ws-jsonrpc` examples here:
6
7- [client](/packages/examples/src/client)
8- [server](/packages/examples/src/server)
9
10## Client side connection handling
11
12```ts
13import { MessageConnection, NotificationType } from 'vscode-jsonrpc';
14import { listen } from 'vscode-ws-jsonrpc';
15
16const webSocket = new WebSocket('ws://www.example.com/socketserver');
17listen({
18 webSocket,
19 onConnection: (connection: MessageConnection) => {
20 const notification = new rpc.NotificationType<string, void>('testNotification');
21 connection.listen();
22 connection.sendNotification(notification, 'Hello World');
23 }
24});
25```
26
27## Server side connection handling
28
29```ts
30import { createWebSocketConnection, ConsoleLogger, IWebSocket } from 'vscode-ws-jsonrpc';
31import { NotificationType } from 'vscode-languageserver';
32
33const socket: IWebSocket; // open the web socket
34const logger = new ConsoleLogger();
35const connection = createWebSocketConnection(socket, logger);
36const notification = new NotificationType<string, void>('testNotification');
37connection.onNotification(notification, (param: string) => {
38 console.log(param); // This prints Hello World
39});
40
41connection.listen();
42```
43
44## Server side connection forwarding
45
46```ts
47import { IWebSocket, WebSocketMessageReader, WebSocketMessageWriter } from 'vscode-ws-jsonrpc';
48import { createConnection, createServerProcess, forward } from 'vscode-ws-jsonrpc/server';
49import { Message } from 'vscode-languageserver';
50
51const socket: IWebSocket; // open the web socket
52const reader = new WebSocketMessageReader(socket);
53const writer = new WebSocketMessageWriter(socket);
54const socketConnection = createConnection(reader, writer, () => socket.dispose())
55const serverConnection = createServerProcess('Example', 'node', ['example.js']);
56forward(socketConnection, serverConnection, message => {
57 if (Message.isNotification(message)) {
58 if (message.method === 'testNotification') {
59 // handle the test notification
60 }
61 }
62 return message;
63});
64```
65
66## History
67
68For the history please see the [README](https://github.com/TypeFox/monaco-languageclient/blob/main/README.md#latest-important-project-changes) and [CHANGELOG](https://github.com/TypeFox/monaco-languageclient/blob/main/packages/vscode-ws-jsonrpc/CHANGELOG.md).
69
70## License
71
72[MIT](https://github.com/TypeFox/monaco-languageclient/blob/main/packages/vscode-ws-jsonrpc/License.txt)