# feixuekeji/flysocket architecture

> Architecture diagram of the GitHub repository feixuekeji/flysocket, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-25). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/feixuekeji/flysocket
- Repository: https://github.com/feixuekeji/flysocket

## Overview

##### Project type and purpose
- This is a WebSocket-based MVC framework (“flysocket”) built on top of GatewayWorker/Workerman.
- Its goal is to provide a unified long-connection interface (Web, App, IoT) that stays resident in memory, avoiding repeated FPM bootstraps.
- It follows MVC conventions inspired by ThinkPHP and integrates ThinkORM, Redis caching, logging, validation, and token auth.

##### File structure
Top-level components and their roles:
· application/… – contains your MVC controllers, models and validators (admin and index modules)
· lib/… – core framework classes (App, Autoloader, Container, Facade, Route, Request, Session, Cache, Log, Validate, Error)
· extend/Token.php – JWT or token-based authentication helper
· config/… – all runtime configuration (database, cache, session, logging, custom cron, common constants)
· route/route.php – WebSocket route definitions or controller bindings
· worker/… – GatewayWorker startup scripts (start_gateway.php, start_businessworker.php, start_register.php, start_http_server.php, plus Events.php)
· socket.js, ws.html – sample front-end WebSocket client implementations
· start.php / start_for_win.bat – bootstrap scripts for launching all worker processes
· Dockerfile – containerization for the entire stack

##### README for additional insights
- Emphasizes GatewayWorker’s three roles (Gateway, BusinessWorker, Register)
- Mentions long-connection JSON messaging, MVC, ThinkORM integration, facades, validators

##### Main components and interactions
A. Clients
- Browser/Web (ws.html)
- Node/JS apps (socket.js)
- Mobile/IoT devices
B. GatewayWorker cluster
1. Gateway processes
· Listens on WS/TCP port
· Terminates raw sockets, handles handshake, heartbeat
2. Register service
· Keeps track of active BusinessWorkers and Gateways
· Facilitates service discovery
3. BusinessWorker processes
· Loads the PHP MVC framework (lib/ + application/)
· Runs your controllers (application/*/controller) and models (ThinkORM)
· Uses facades to access Cache, Log, Request, Session, Route
C. HTTP server process (optional: start_http_server.php)
- Exposes HTTP endpoints (maybe for health checks, static assets)
D. Supporting services
- MySQL (config/database.php via ThinkORM)
- Redis (config/cache.php, lib/Redis.php)
- File/system logging (config/log.php, lib/Log.php)
E. Shared configuration and utilities
- Config container (lib/Config.php)
- Dependency injection / service container (lib/Container.php)
- Token auth (extend/Token.php)

##### Architectural patterns and design principles
- MVC: clear separation of controllers, models, validation
- Facade pattern: static proxies to services (lib/facade/*.php)
- DI/Service Container: resolves dependencies via Container.php
- Process separation: Gateway vs BusinessWorker vs Register vs HTTP
- JSON message format over WebSocket
- ThinkORM integration for database abstraction
- Token-based authentication extension

– Gateway nodes (rectangle “Gateway (Workerman)”)
– Register service (rectangle “Register”)
– BusinessWorker nodes (rectangle “BusinessWorker + flysocket MVC”)
c. External services on the right:
- Clients → Gateway (WebSocket handshake & messages)
- Gateway ↔ Register (service discovery)
- Gateway → BusinessWorker (message dispatch)
- BusinessWorker → MySQL / Redis / Log (via lib classes & config)
- BusinessWorker → Gateway → Clients (responses/events)

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Clients" 
        direction TB
        Browser["Browser (ws.html)"]:::network
        NodeJS["Node.js (socket.js)"]:::network
        IoT["Mobile/IoT Devices"]:::network
    end

    subgraph "GatewayWorker Cluster"
        direction TB
        GWWS["Gateway (WebSocket)"]:::network
        GWText["Gateway (Text Protocol)"]:::network
        Register["Register Service"]:::network
        BW["BusinessWorker + flysocket MVC"]:::app
        HTTP["HTTP Server (Optional)"]:::network
    end

    subgraph "Supporting Services"
        direction TB
        MySQL["MySQL (ThinkORM)"]:::data
        Redis["Redis (Cache/Session)"]:::data
        Logs["Log Files"]:::data
    end

    subgraph "Utilities / Config"
        direction TB
        Config["Configuration Manager"]:::util
        Container["Dependency Injection Container"]:::util
        Token["Token Authentication"]:::util
        Validate["Validation Engine"]:::util
    end

    Browser -->|"WebSocket"| GWWS
    NodeJS -->|"WebSocket"| GWWS
    IoT -->|"WebSocket"| GWWS

    GWWS -->|"register() / onConnect()"| Register
    GWText -->|"register() / onConnect()"| Register
    Register -->|"lookup()"| GWWS
    Register -->|"lookup()"| GWText

    GWWS -->|"dispatch() / onMessage()"| BW
    GWText -->|"dispatch() / onMessage()"| BW

    BW -->|"optional HTTP"| HTTP

    BW -->|"Query/Update"| MySQL
    BW -->|"Cache/Session"| Redis
    BW -->|"Logging"| Logs

    BW -->|"uses"| Config
    BW -->|"uses"| Container
    BW -->|"uses"| Token
    BW -->|"uses"| Validate

    click Browser "https://github.com/feixuekeji/flysocket/blob/master/ws.html"
    click NodeJS "https://github.com/feixuekeji/flysocket/blob/master/socket.js"
    click GWWS "https://github.com/feixuekeji/flysocket/blob/master/worker/start_gateway.php"
    click GWText "https://github.com/feixuekeji/flysocket/blob/master/worker/start_text_gateway.php"
    click Register "https://github.com/feixuekeji/flysocket/blob/master/worker/start_register.php"
    click BW "https://github.com/feixuekeji/flysocket/blob/master/worker/start_businessworker.php"
    click HTTP "https://github.com/feixuekeji/flysocket/blob/master/worker/start_http_server.php"
    click Config "https://github.com/feixuekeji/flysocket/blob/master/lib/Config.php"
    click Container "https://github.com/feixuekeji/flysocket/blob/master/lib/Container.php"
    click Token "https://github.com/feixuekeji/flysocket/blob/master/extend/Token.php"
    click Validate "https://github.com/feixuekeji/flysocket/blob/master/lib/Validate.php"

    classDef network fill:#C0E4FF,stroke:#333,stroke-width:1px
    classDef app fill:#C8F7C5,stroke:#333,stroke-width:1px
    classDef data fill:#FFF7C0,stroke:#333,stroke-width:1px
    classDef util fill:#E0E0E0,stroke:#333,stroke-width:1px
```
