Skip to content

licheng1013/rocket-cat

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

288 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rocket Cat

Rocket Cat is a small Go WebSocket action framework built around explicit routes:

route = (cmd << 16) | subCmd

The project favors simple module boundaries, explicit registration, unified request binding, and unified responses.

Layout

/ws
    bind.go
    context.go
    packet.go
    response.go
    router.go
    server.go
    session.go

/modules
    /user
        cmd.go
        model.go
        handler.go
        service.go
    /chat
        cmd.go
        model.go
        handler.go
        service.go
    /room
        cmd.go
        model.go
        handler.go
        service.go

/main.go

Run

go run .

The WebSocket endpoint is:

ws://localhost:10100/ws

Example Client

Open examples/ws-client.html in a browser after starting the server. It uses Vue from a CDN and can simulate user, chat, and room packets.

Rules for maintaining example pages are documented in docs/ws-client-rules.md.

Open examples/frame-sync.html in two browser windows to simulate frame synchronization. Confirm different IDs, join matching from both windows, and the room will start syncing after two clients are matched.

Packet

Requests use JSON:

{
  "cmd": 1001,
  "subCmd": 1,
  "data": {
    "account": "demo",
    "password": "123456"
  }
}

Responses use:

{
  "cmd": 1001,
  "subCmd": 1,
  "code": 0,
  "msg": "ok",
  "data": {}
}

Module Rules

Each module must contain:

cmd.go      command and route definitions only
model.go    request and response structs only
handler.go  bind request, call service, return response
service.go  core business logic only

Handlers should use ws.Bind[T](ctx) instead of calling json.Unmarshal directly.

Modules register routes explicitly:

func Register(router *ws.Router) {
	router.Register(Cmd, Login, LoginHandler)
}

Example Handler

func LoginHandler(ctx *ws.Context) {
	req, err := ws.Bind[LoginReq](ctx)
	if err != nil {
		return
	}

	resp, err := LoginService(req)
	if err != nil {
		ws.Fail(ctx, 1, err.Error())
		return
	}

	ws.OK(ctx, resp)
}

Tests

go test ./...

About

一个简单的tcp/kcp/ws封装框架.

Resources

License

Stars

2 stars

Watchers

1 watching

Forks

Packages

 
 
 

Contributors

Languages