/pipi-gf-demo

Primary LanguageGoMIT LicenseMIT

FOR PIPI

初始化

1. 安装Go环境

2. 下载代码git clone

git clone https://github.com/waiter/pipi-gf-demo

3. 将manifest/sql/create.sql导入数据库,创建user表

4. 修改manifest/config/config.yaml中数据库配置

5. 安装以来go mod tidy

6. 使用go run main.go或者make dev来启动,正常情况可以看到以下日志:

  ADDRESS | METHOD |        ROUTE         |                                  HANDLER                                   |         MIDDLEWARE           
----------|--------|----------------------|----------------------------------------------------------------------------|------------------------------
  :8199   | ALL    | /*                   | github.com/gogf/gf/v2/net/ghttp.internalMiddlewareServerTracing            | GLOBAL MIDDLEWARE            
----------|--------|----------------------|----------------------------------------------------------------------------|------------------------------
  :8199   | ALL    | /*                   | github.com/gogf/gf/v2/net/ghttp.MiddlewareHandlerResponse                  | GLOBAL MIDDLEWARE            
----------|--------|----------------------|----------------------------------------------------------------------------|------------------------------
  :8199   | ALL    | /api.json            | github.com/gogf/gf/v2/net/ghttp.(*Server).openapiSpec                      |                              
----------|--------|----------------------|----------------------------------------------------------------------------|------------------------------
  :8199   | ALL    | /swagger/*           | github.com/gogf/gf/v2/net/ghttp.(*Server).swaggerUI                        | HOOK_BEFORE_SERVE            
----------|--------|----------------------|----------------------------------------------------------------------------|------------------------------
  :8199   | POST   | /user/check-passport | pipi.com/gogf/pipi-gf-demo/internal/controller.(*cUser).CheckPassport | service.(*sMiddleware).Ctx   
          |        |                      |                                                                            | service.(*sMiddleware).CORS  
----------|--------|----------------------|----------------------------------------------------------------------------|------------------------------
  :8199   | POST   | /user/is-signed-in   | pipi.com/gogf/pipi-gf-demo/internal/controller.(*cUser).IsSignedIn    | service.(*sMiddleware).Ctx   
          |        |                      |                                                                            | service.(*sMiddleware).CORS  
----------|--------|----------------------|----------------------------------------------------------------------------|------------------------------
  :8199   | GET    | /user/profile        | pipi.com/gogf/pipi-gf-demo/internal/controller.(*cUser).Profile       | service.(*sMiddleware).Ctx   
          |        |                      |                                                                            | service.(*sMiddleware).CORS  
          |        |                      |                                                                            | service.(*sMiddleware).Auth  
----------|--------|----------------------|----------------------------------------------------------------------------|------------------------------
  :8199   | POST   | /user/sign-in        | pipi.com/gogf/pipi-gf-demo/internal/controller.(*cUser).SignIn        | service.(*sMiddleware).Ctx   
          |        |                      |                                                                            | service.(*sMiddleware).CORS  
----------|--------|----------------------|----------------------------------------------------------------------------|------------------------------
  :8199   | POST   | /user/sign-out       | pipi.com/gogf/pipi-gf-demo/internal/controller.(*cUser).SignOut       | service.(*sMiddleware).Ctx   
          |        |                      |                                                                            | service.(*sMiddleware).CORS  
----------|--------|----------------------|----------------------------------------------------------------------------|------------------------------
  :8199   | POST   | /user/sign-up        | pipi.com/gogf/pipi-gf-demo/internal/controller.(*cUser).SignUp        | service.(*sMiddleware).Ctx   
          |        |                      |                                                                            | service.(*sMiddleware).CORS  
----------|--------|----------------------|----------------------------------------------------------------------------|------------------------------

多环境配置

当前支持两种环境:

  • 开发环境:
    • 配置文件:manifest/config/config.yaml
    • 启动命令:go run main.go或者make dev
  • 正式环境:
    • 配置文件:manifest/config/config.prod.yaml
    • 启动命令:go run main.go --gf.gcfg.file=config.prod.yaml或者make prod

WebSocket相关说明

如何调试

启动服务后,访问http://localhost:8199即可,当前需要先创建一个用户名为pipi,密码为123456的帐号

关于心跳

  • 当前需要前端每隔一定时间(比internal/consts/consts.go中定义的WebSocketHeartBeatTime短即可)给服务端发送心跳包,直接发送内容为ping的包即可
  • 另外,所有前端发送的包(不只是心跳包)都会刷新后端记录本WebSocket的超时时间

消息格式

前端

前端给后端主要以JSON形式传递

  • cmd:本次命令类型
  • data: 本次携带数据

例如:

{"cmd":"testAdd","data":[1,78,3]}

后端

后端回包格式与前端一致,会额外增加:

  • unique:本WebSocket唯一ID
  • pack:服务端发出时间

例如

{"cmd":"add","data":82,"pack":1648906839,"unique":"ws-RFbD56T"}

另外,后端只需要在internal/service/socket/webLogic.go中添加相关逻辑即可

ClientSocket相关

如何调试

启动服务后,到client目录下go run .即可启动简易的逻辑。另外,ClientSocket的端口配置在config.yaml

其他

其他设定与WebSocket基本一致