This commit is contained in:
2022-05-04 11:13:09 +08:00
parent 3d331ee10d
commit 0b76b5935d
68 changed files with 38788 additions and 1 deletions

View File

@@ -0,0 +1,26 @@
import { ApiCall } from "tsrpc";
import { server } from "..";
import { ReqSend, ResSend } from "../shared/protocols/PtlSend";
// This is a demo code file
// Feel free to delete it
export async function ApiSend(call: ApiCall<ReqSend, ResSend>): Promise<void> {
// Error
if (call.req.content.length === 0) {
call.error("Content is empty");
return;
}
// Success
let time: Date = new Date();
call.succ({
time: time
});
// Broadcast
server.broadcastMsg("Chat", {
content: "Chat: " + call.req.content,
time: time
});
}

View File

@@ -0,0 +1,31 @@
import dayjs from "dayjs";
import { ApiCall } from "tsrpc";
import { server } from "../..";
import { Reqstart, Resstart } from "../../shared/protocols/lobby/Ptlstart";
export async function Apistart(call: ApiCall<Reqstart, Resstart>): Promise<void> {
// Error
// if (call.req.content.length === 0) {
// call.error("Content is empty");
// return;
// }
// Success
let time: Date = new Date();
let dayjstime: string = dayjs().format("YYYY-MM-DD HH:mm:ss");
call.logger.log(`${dayjstime} Apistart`);
call.succ({
code: 1,
data: {
time: dayjstime
}
});
// Broadcast
server.broadcastMsg("lobby/B_start", {
time: time,
data: {
content: `${dayjstime}: B_start`
}
});
}

32
backend/src/index.ts Normal file
View File

@@ -0,0 +1,32 @@
import dayjs from "dayjs";
import * as path from "path";
import { WsServer } from "tsrpc";
import { serviceProto, ServiceType } from "./shared/protocols/serviceProto";
require("dotenv").config();
require("dayjs/locale/zh-tw");
dayjs.locale("zh-tw");
// Create the Server
export const server: WsServer<ServiceType> = new WsServer(serviceProto, {
port: +process.env.PORT! || 3000,
// Remove this to use binary mode (remove from the client too)
json: true
});
// Initialize before server start
async function init(): Promise<void> {
await server.autoImplementApi(path.resolve(__dirname, "api"));
// TODO
// let time: Date = new Date();
let time: string = dayjs().format("YYYY-MM-DD HH:mm:ss");
console.log(`${time} Start Server`);
// Prepare something... (e.g. connect the db)
}
// Entry function
async function main(): Promise<void> {
await init();
await server.start();
}
main();

View File

@@ -0,0 +1,7 @@
// This is a demo code file
// Feel free to delete it
export interface MsgChat {
content: string;
time: Date;
}

View File

@@ -0,0 +1,11 @@
// This is a demo code file
// Feel free to delete it
export interface ReqSend {
content: string;
}
export interface ResSend {
time: Date;
}

View File

@@ -0,0 +1,15 @@
export interface BaseRequest {
}
export interface BaseResponse {
}
export interface BaseConf {
}
export interface BaseMessage {
}

View File

@@ -0,0 +1,9 @@
// This is a demo code file
// Feel free to delete it
export interface MsgB_start {
data: {
content: string
};
time: Date;
}

View File

@@ -0,0 +1,11 @@
// This is a demo code file
// Feel free to delete it
export interface Reqstart { }
export interface Resstart {
code: number; // 与 TSRPC 的错误处理重复
data: {
time: string;
};
}

View File

@@ -0,0 +1,152 @@
import { ServiceProto } from 'tsrpc-proto';
import { MsgB_start } from './lobby/MsgB_start';
import { Reqstart, Resstart } from './lobby/Ptlstart';
import { MsgChat } from './MsgChat';
import { ReqSend, ResSend } from './PtlSend';
export interface ServiceType {
api: {
"lobby/start": {
req: Reqstart,
res: Resstart
},
"Send": {
req: ReqSend,
res: ResSend
}
},
msg: {
"lobby/B_start": MsgB_start,
"Chat": MsgChat
}
}
export const serviceProto: ServiceProto<ServiceType> = {
"version": 1,
"services": [
{
"id": 5,
"name": "lobby/B_start",
"type": "msg"
},
{
"id": 1,
"name": "lobby/start",
"type": "api"
},
{
"id": 2,
"name": "Chat",
"type": "msg"
},
{
"id": 4,
"name": "Send",
"type": "api"
}
],
"types": {
"lobby/MsgB_start/MsgB_start": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "data",
"type": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "content",
"type": {
"type": "String"
}
}
]
}
},
{
"id": 1,
"name": "time",
"type": {
"type": "Date"
}
}
]
},
"lobby/Ptlstart/Reqstart": {
"type": "Interface"
},
"lobby/Ptlstart/Resstart": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "code",
"type": {
"type": "Number"
}
},
{
"id": 1,
"name": "data",
"type": {
"type": "Interface",
"properties": [
{
"id": 0,
"name": "time",
"type": {
"type": "String"
}
}
]
}
}
]
},
"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"
}
}
]
}
}
};