1 | # VSCode WebSocket JSON RPC
|
2 |
|
3 | NPM module to implement communication between a jsonrpc client and server over WebSocket.
|
4 |
|
5 | See 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
|
13 | import { MessageConnection, NotificationType } from 'vscode-jsonrpc';
|
14 | import { listen } from 'vscode-ws-jsonrpc';
|
15 |
|
16 | const webSocket = new WebSocket('ws://www.example.com/socketserver');
|
17 | listen({
|
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
|
30 | import { createWebSocketConnection, ConsoleLogger, IWebSocket } from 'vscode-ws-jsonrpc';
|
31 | import { NotificationType } from 'vscode-languageserver';
|
32 |
|
33 | const socket: IWebSocket; // open the web socket
|
34 | const logger = new ConsoleLogger();
|
35 | const connection = createWebSocketConnection(socket, logger);
|
36 | const notification = new NotificationType<string, void>('testNotification');
|
37 | connection.onNotification(notification, (param: string) => {
|
38 | console.log(param); // This prints Hello World
|
39 | });
|
40 |
|
41 | connection.listen();
|
42 | ```
|
43 |
|
44 | ## Server side connection forwarding
|
45 |
|
46 | ```ts
|
47 | import { IWebSocket, WebSocketMessageReader, WebSocketMessageWriter } from 'vscode-ws-jsonrpc';
|
48 | import { createConnection, createServerProcess, forward } from 'vscode-ws-jsonrpc/server';
|
49 | import { Message } from 'vscode-languageserver';
|
50 |
|
51 | const socket: IWebSocket; // open the web socket
|
52 | const reader = new WebSocketMessageReader(socket);
|
53 | const writer = new WebSocketMessageWriter(socket);
|
54 | const socketConnection = createConnection(reader, writer, () => socket.dispose())
|
55 | const serverConnection = createServerProcess('Example', 'node', ['example.js']);
|
56 | forward(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 |
|
68 | For 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)
|