-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(example): add rpc-websockets example
- Loading branch information
Showing
6 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package-lock.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Deepkit RPC WebSockets Example | ||
|
||
This example shows how to use the Deepkit RPC over WebSockets. | ||
|
||
## How to run | ||
|
||
First install dependencies | ||
|
||
``` | ||
# postinstall script automatically installs deepkit type compiler | ||
npm install | ||
``` | ||
|
||
Start server: | ||
|
||
``` | ||
./node_modules/.bin/ts-node server.ts | ||
``` | ||
|
||
Start client: | ||
|
||
``` | ||
./node_modules/.bin/ts-node client.ts | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { RpcWebSocketClient } from '@deepkit/rpc'; | ||
import type { Controller } from './server'; | ||
|
||
async function main() { | ||
const client = new RpcWebSocketClient('ws://127.0.0.1:8081'); | ||
const controller = client.controller<Controller>('/main'); | ||
|
||
const result = await controller.hello('World'); | ||
console.log('result', result); | ||
|
||
client.disconnect(); | ||
} | ||
|
||
main().catch(console.error); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "deepkit-example-rpc-websockets", | ||
"version": "0.0.1", | ||
"scripts": { | ||
"postinstall": "deepkit-type-install" | ||
}, | ||
"dependencies": { | ||
"ws": "^8.13.0", | ||
"@deepkit/type": "^1.0.1-alpha.97", | ||
"@deepkit/rpc": "^1.0.1-alpha.97", | ||
"@deepkit/rpc-tcp": "^1.0.1-alpha.97" | ||
}, | ||
"devDependencies": { | ||
"@deepkit/type-compiler": "^1.0.1-alpha.97", | ||
"typescript": "^5.2.2", | ||
"ts-node": "^10.9.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { rpc, RpcKernel } from '@deepkit/rpc'; | ||
import { RpcWebSocketServer } from '@deepkit/rpc-tcp'; | ||
|
||
@rpc.controller('/main') | ||
export class Controller { | ||
@rpc.action() | ||
hello(title: string): string { | ||
return 'Hello ' + title; | ||
} | ||
} | ||
|
||
const kernel = new RpcKernel(); | ||
kernel.registerController(Controller); | ||
const server = new RpcWebSocketServer(kernel, 'localhost:8081'); | ||
server.start({ | ||
host: '127.0.0.1', | ||
port: 8081, | ||
}); | ||
console.log('Server started at ws://127.0.0.1:8081'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"compilerOptions": { | ||
"strict": true, | ||
"experimentalDecorators": true, | ||
"moduleResolution": "node", | ||
"target": "ES2022", | ||
"esModuleInterop": true, | ||
"module": "CommonJS" | ||
}, | ||
"reflection": true | ||
} |