[mod]
This commit is contained in:
26
backend/src/api/ApiSend.ts
Normal file
26
backend/src/api/ApiSend.ts
Normal 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
|
||||
});
|
||||
}
|
31
backend/src/api/lobby/Apistart.ts
Normal file
31
backend/src/api/lobby/Apistart.ts
Normal 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
32
backend/src/index.ts
Normal 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();
|
7
backend/src/shared/protocols/MsgChat.ts
Normal file
7
backend/src/shared/protocols/MsgChat.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
// This is a demo code file
|
||||
// Feel free to delete it
|
||||
|
||||
export interface MsgChat {
|
||||
content: string;
|
||||
time: Date;
|
||||
}
|
11
backend/src/shared/protocols/PtlSend.ts
Normal file
11
backend/src/shared/protocols/PtlSend.ts
Normal 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;
|
||||
}
|
15
backend/src/shared/protocols/base.ts
Normal file
15
backend/src/shared/protocols/base.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
export interface BaseRequest {
|
||||
|
||||
}
|
||||
|
||||
export interface BaseResponse {
|
||||
|
||||
}
|
||||
|
||||
export interface BaseConf {
|
||||
|
||||
}
|
||||
|
||||
export interface BaseMessage {
|
||||
|
||||
}
|
9
backend/src/shared/protocols/lobby/MsgB_start.ts
Normal file
9
backend/src/shared/protocols/lobby/MsgB_start.ts
Normal 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;
|
||||
}
|
11
backend/src/shared/protocols/lobby/Ptlstart.ts
Normal file
11
backend/src/shared/protocols/lobby/Ptlstart.ts
Normal 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;
|
||||
};
|
||||
}
|
152
backend/src/shared/protocols/serviceProto.ts
Normal file
152
backend/src/shared/protocols/serviceProto.ts
Normal 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"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
};
|
Reference in New Issue
Block a user