init
This commit is contained in:
		
							
								
								
									
										26
									
								
								examples/cocos-room-cluster/backend/src/api/ApiSend.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								examples/cocos-room-cluster/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>) { | ||||
|     // 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 | ||||
|     }) | ||||
| } | ||||
							
								
								
									
										25
									
								
								examples/cocos-room-cluster/backend/src/index.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								examples/cocos-room-cluster/backend/src/index.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,25 @@ | ||||
| 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, | ||||
|     // Remove this to use binary mode (remove from the client too) | ||||
|     json: true | ||||
| }); | ||||
|  | ||||
| // Initialize before server start | ||||
| async function init() { | ||||
|     await server.autoImplementApi(path.resolve(__dirname, 'api')); | ||||
|  | ||||
|     // TODO | ||||
|     // Prepare something... (e.g. connect the db) | ||||
| }; | ||||
|  | ||||
| // Entry function | ||||
| async function main() { | ||||
|     await init(); | ||||
|     await server.start(); | ||||
| } | ||||
| main(); | ||||
| @@ -0,0 +1,7 @@ | ||||
| // This is a demo code file | ||||
| // Feel free to delete it | ||||
|  | ||||
| export interface MsgChat { | ||||
|     content: string, | ||||
|     time: Date | ||||
| } | ||||
| @@ -0,0 +1,10 @@ | ||||
| // This is a demo code file | ||||
| // Feel free to delete it | ||||
|  | ||||
| export interface ReqSend { | ||||
|     content: string | ||||
| } | ||||
|  | ||||
| export interface ResSend { | ||||
|     time: Date | ||||
| } | ||||
| @@ -0,0 +1,15 @@ | ||||
| export interface BaseRequest { | ||||
|      | ||||
| } | ||||
|  | ||||
| export interface BaseResponse { | ||||
|      | ||||
| } | ||||
|  | ||||
| export interface BaseConf { | ||||
|      | ||||
| } | ||||
|  | ||||
| export interface BaseMessage { | ||||
|  | ||||
| } | ||||
| @@ -0,0 +1,78 @@ | ||||
| import { ServiceProto } from 'tsrpc-proto'; | ||||
| import { MsgChat } from './MsgChat'; | ||||
| import { ReqSend, ResSend } from './PtlSend'; | ||||
|  | ||||
| // This is a demo service proto file (auto generated) | ||||
| // Feel free to delete it | ||||
|  | ||||
| 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" | ||||
|                     } | ||||
|                 } | ||||
|             ] | ||||
|         } | ||||
|     } | ||||
| }; | ||||
		Reference in New Issue
	
	Block a user