TSRPC Implementation Examples

repository·main·Indexed 43 days ago

https://github.com/k8w/tsrpc-examples

A collection of implementation examples for TSRPC, demonstrating how to build and connect backends and frontends across various environments. Includes demos for chatroom apps, serverless functions, MongoDB CRUD, file uploads, and Cocos Creator integrations, including a multiplayer real-time combat game and distributed room management architecture.

Tokens
3.8K
Snippets
37
Records
40
Agent score
54%

What's inside TSRPC Examples

  1. Overview of the Distributed Room Management Architecture Example

    main

    This project demonstrates a lightweight, distributed architecture for room management (such as creating rooms and random matchmaking) using TSRPC. It is designed as a replacement for MGOBE and focuses on scalability and ease of use compared to traditional three-tier architectures.

    Key Features

    • Room Management: Supports creating rooms and random matchmaking.
    • Modular Logic: Room logic is encapsulated into separate Classes for easy customization.
    • Distributed & Scalable: Supports horizontal scaling and distributed deployment.
    • Low Barrier to Entry: Simpler structure than traditional three-tier architectures.
  2. Setup the Frontend for Cocos Creator

    main

    To use the frontend part of this demo, you must use Cocos Creator version 3.5.2 or higher.

    Important Workflow:

    1. Navigate to the frontend directory.
    2. Run npm install to install dependencies.
    3. Open the project in Cocos Creator.

    Note: If you encounter errors when opening the project, try closing Cocos Creator and restarting it after the npm install step is complete.

  3. Manage TSRPC backend code generation and synchronization

    main

    The backend provides several scripts to manage the TSRPC development lifecycle, specifically for handling protocol definitions and keeping the client in sync with the server's API definitions.

    • Generate ServiceProto: Run npm run proto to generate the service protocol definitions.
    • Generate API templates: Run npm run api to generate the necessary API templates.
    • Sync shared code to client: Run npm run sync to synchronize shared code (like types or interfaces) to the client.

    Note: If you used the symlink option when initializing your project with create-tsrpc-app, the sync command will re-create the symlink instead of copying files.

    npm run proto
    npm run api
    npm run sync
  4. Setup the Cocos Creator Frontend

    main

    The frontend is built for Cocos Creator. Follow these steps to set up the environment:

    1. Requirements: Cocos Creator version 3.4.2 or higher.
    2. Installation: Run npm install in the project directory, then open the project in Cocos Creator.
    3. Troubleshooting: If Cocos Creator reports errors after npm install, perform a clean restart:
      • Close Cocos Creator.
      • Delete the library and temp directories.
      • Restart Cocos Creator.
    npm install
  5. Launch the TSRPC + Cocos Multiplayer Demo

    main

    To run the multiplayer real-time matchmaking game demo, you must start the backend services before opening the frontend in Cocos Creator. The architecture uses a separate logic and presentation model, employing prediction, reconciliation, and interpolation to handle network delays of 100~200 ms.

    ### 1. Launch the backend
    cd backend
    npm install
    npm run dev
    
    ### 2. Launch the frontend
    cd frontend
    npm install
    # Open the 'frontend' directory with Cocos Creator (v3.5.2+)