chatroom
This commit is contained in:
3
examples/chatroom/backend/.gitignore
vendored
Normal file
3
examples/chatroom/backend/.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
node_modules
|
||||
dist
|
||||
.DS_STORE
|
30
examples/chatroom/backend/.vscode/launch.json
vendored
Normal file
30
examples/chatroom/backend/.vscode/launch.json
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
{
|
||||
"configurations": [
|
||||
{
|
||||
"type": "node",
|
||||
"request": "launch",
|
||||
"name": "mocha current file",
|
||||
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
|
||||
"args": [
|
||||
"${file}"
|
||||
],
|
||||
"internalConsoleOptions": "openOnSessionStart",
|
||||
"cwd": "${workspaceFolder}"
|
||||
},
|
||||
{
|
||||
"type": "node",
|
||||
"request": "launch",
|
||||
"name": "ts-node current file",
|
||||
"protocol": "inspector",
|
||||
"args": [
|
||||
"${relativeFile}"
|
||||
],
|
||||
"cwd": "${workspaceRoot}",
|
||||
"runtimeArgs": [
|
||||
"-r",
|
||||
"ts-node/register"
|
||||
],
|
||||
"internalConsoleOptions": "openOnSessionStart"
|
||||
}
|
||||
]
|
||||
}
|
3
examples/chatroom/backend/.vscode/settings.json
vendored
Normal file
3
examples/chatroom/backend/.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"typescript.tsdk": "node_modules\\typescript\\lib"
|
||||
}
|
35
examples/chatroom/backend/README.md
Normal file
35
examples/chatroom/backend/README.md
Normal file
@@ -0,0 +1,35 @@
|
||||
# TSRPC Server
|
||||
|
||||
## Run
|
||||
### Local Dev Server
|
||||
```
|
||||
npm run dev
|
||||
```
|
||||
|
||||
|
||||
|
||||
### Build
|
||||
```
|
||||
npm run build
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Files
|
||||
### Generate ServiceProto
|
||||
```
|
||||
npm run proto
|
||||
```
|
||||
|
||||
### Generate API templates
|
||||
```
|
||||
npm run api
|
||||
```
|
||||
|
||||
### Sync shared code to client
|
||||
|
||||
```
|
||||
npm run sync
|
||||
```
|
||||
|
||||
> If you chose symlink when using `create-tsrpc-app`, it would re-create the symlink instead of copy files.
|
23
examples/chatroom/backend/package.json
Normal file
23
examples/chatroom/backend/package.json
Normal file
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"name": "chatroom-backend",
|
||||
"version": "0.1.0",
|
||||
"main": "index.js",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"proto": "tsrpc proto -i src/shared/protocols -o src/shared/protocols/serviceProto.ts",
|
||||
"sync": "tsrpc sync --from src/shared --to ../frontend/src/shared",
|
||||
"api": "tsrpc api -i src/shared/protocols/serviceProto.ts -o src/api",
|
||||
"dev": "onchange \"src/**/*.ts\" -i -k -- ts-node \"src/index.ts\"",
|
||||
"build": "tsrpc build"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^15.12.2",
|
||||
"onchange": "^7.1.0",
|
||||
"ts-node": "^9.1.1",
|
||||
"tsrpc-cli": "^2.0.1-dev.11",
|
||||
"typescript": "^4.3.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"tsrpc": "^3.0.0-dev.19"
|
||||
}
|
||||
}
|
23
examples/chatroom/backend/src/api/ApiSend.ts
Normal file
23
examples/chatroom/backend/src/api/ApiSend.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { ApiCall } from "tsrpc";
|
||||
import { server } from "..";
|
||||
import { ReqSend, ResSend } from "../shared/protocols/PtlSend";
|
||||
|
||||
export async function ApiSend(call: ApiCall<ReqSend, ResSend>) {
|
||||
// Error
|
||||
if (call.req.content.length === 0) {
|
||||
call.error('Content is empty')
|
||||
return;
|
||||
}
|
||||
|
||||
// Success
|
||||
let time = new Date();
|
||||
call.succ({
|
||||
time: time
|
||||
});
|
||||
|
||||
// Broadcast
|
||||
server.broadcastMsg('Chat', {
|
||||
content: call.req.content,
|
||||
time: time
|
||||
})
|
||||
}
|
24
examples/chatroom/backend/src/index.ts
Normal file
24
examples/chatroom/backend/src/index.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import * as path from "path";
|
||||
import { WsServer } from "tsrpc";
|
||||
import { serviceProto } from './shared/protocols/serviceProto';
|
||||
|
||||
// Create the Server
|
||||
export const server = new WsServer(serviceProto, {
|
||||
port: 3000
|
||||
});
|
||||
|
||||
// Entry function
|
||||
async function main() {
|
||||
await server.autoImplementApi(path.resolve(__dirname, 'api'));
|
||||
|
||||
// TODO
|
||||
// Prepare something... (e.g. connect the db)
|
||||
|
||||
await server.start();
|
||||
};
|
||||
|
||||
main().catch(e => {
|
||||
// Exit if any error during the startup
|
||||
server.logger.error(e);
|
||||
process.exit(-1);
|
||||
});
|
@@ -0,0 +1,4 @@
|
||||
export interface MsgChat {
|
||||
content: string,
|
||||
time: Date
|
||||
}
|
@@ -0,0 +1,7 @@
|
||||
export interface ReqSend {
|
||||
content: string
|
||||
}
|
||||
|
||||
export interface ResSend {
|
||||
time: Date
|
||||
}
|
@@ -0,0 +1,75 @@
|
||||
import { ServiceProto } from 'tsrpc-proto';
|
||||
import { MsgChat } from './MsgChat';
|
||||
import { ReqSend, ResSend } from './PtlSend';
|
||||
|
||||
export interface ServiceType {
|
||||
api: {
|
||||
"Send": {
|
||||
req: ReqSend,
|
||||
res: ResSend
|
||||
}
|
||||
},
|
||||
msg: {
|
||||
"Chat": MsgChat
|
||||
}
|
||||
}
|
||||
|
||||
export const serviceProto: ServiceProto<ServiceType> = {
|
||||
"services": [
|
||||
{
|
||||
"id": 0,
|
||||
"name": "Chat",
|
||||
"type": "msg"
|
||||
},
|
||||
{
|
||||
"id": 1,
|
||||
"name": "Send",
|
||||
"type": "api"
|
||||
}
|
||||
],
|
||||
"types": {
|
||||
"MsgChat/MsgChat": {
|
||||
"type": "Interface",
|
||||
"properties": [
|
||||
{
|
||||
"id": 0,
|
||||
"name": "content",
|
||||
"type": {
|
||||
"type": "String"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": 1,
|
||||
"name": "time",
|
||||
"type": {
|
||||
"type": "Date"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"PtlSend/ReqSend": {
|
||||
"type": "Interface",
|
||||
"properties": [
|
||||
{
|
||||
"id": 0,
|
||||
"name": "content",
|
||||
"type": {
|
||||
"type": "String"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"PtlSend/ResSend": {
|
||||
"type": "Interface",
|
||||
"properties": [
|
||||
{
|
||||
"id": 0,
|
||||
"name": "time",
|
||||
"type": {
|
||||
"type": "Date"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
};
|
18
examples/chatroom/backend/tsconfig.json
Normal file
18
examples/chatroom/backend/tsconfig.json
Normal file
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"lib": [
|
||||
"es2018"
|
||||
],
|
||||
"module": "commonjs",
|
||||
"target": "es2018",
|
||||
"outDir": "dist",
|
||||
"strict": true,
|
||||
"esModuleInterop": true,
|
||||
"skipLibCheck": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"moduleResolution": "node"
|
||||
},
|
||||
"include": [
|
||||
"src"
|
||||
]
|
||||
}
|
Reference in New Issue
Block a user