消息服务:
1. 支持 websocket

shizhongqi d37e8679a2 说明文档 il y a 10 mois
.cursor 361e009d31 向 PHP 服务发送未读消息通知 il y a 11 mois
external d23cff9433 修改 UnreadMessageRequest 结构体 il y a 10 mois
logger 85152e9223 删除日志配置中的 FieldsExclude: []string{} il y a 10 mois
wsClient cf0cbffc7d 1.清理模板文件及多余接口 2.区分连接正常断开,还是出现错误 il y a 10 mois
.env.example d37e8679a2 说明文档 il y a 10 mois
.gitignore be3f1a6b2f init commit il y a 11 mois
README.md d37e8679a2 说明文档 il y a 10 mois
config.env.example be3f1a6b2f init commit il y a 11 mois
go.mod 98de475680 日志改使用第三方日志库 zerolog il y a 11 mois
go.sum 98de475680 日志改使用第三方日志库 zerolog il y a 11 mois
main.go cf0cbffc7d 1.清理模板文件及多余接口 2.区分连接正常断开,还是出现错误 il y a 10 mois

README.md

message-server

现有功能

  • 多房间聊天室: 通过 /room?room=<房间名> 动态创建/加入房间,彼此隔离。
  • 实时消息广播: 同一房间内消息通过 WebSocket 即时分发。
  • 昵称: 连接时为生成 clientName (门店端: shop{shopId}{staffId}, 买家端: customer{customId}{userId}), 用于生成客户端的唯一标识,用作唯一标识,进行查找用的

易于扩展的功能(建议优先级)

  • 加入/离开通知与在线人数: 日志记录--用户进入/离开,统计当前在线数。
  • 房间列表与切换: 展示活跃房间、快速切换/创建。
  • 用户自定义昵称: 支持登录前设置昵称,校验重复。
  • 私聊与@提醒: 支持点对点消息、@用户名提醒。
  • 权限与房间控制: 房间密码/邀请制、人数上限、管理员踢人/禁言。
  • 防刷与限流: 消息频率限制、黑名单、基础审核。
  • 富文本/附件: 表情、Markdown、图片/文件上传(结合后端存储)。

服务上线

1. 配置 nginx

创建Nginx WebSocket 配置文件。参考测试环境的 nginx conf 文件 -- chat.shop.huaml.com.conf

server {
    listen 443 ssl;
    server_name chat.shop.huaml.com;

    ssl_certificate     /usr/local/nginx/cert/webSocket/chat.shop.huaml.com.pem;
    ssl_certificate_key /usr/local/nginx/cert/webSocket/chat.shop.huaml.com.key;

    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_read_timeout 120s;   # 读超时(客户端多久没发消息就断开)
    }
}

编译

  1. 摘取代码,执行:go build main.go

  2. 把生成的 main 文件放到线上,并配置好 .env 文件

    LOG_MODE=color
    LOG_LEVEL=0
      
    REDIS_ADDR=118.178.193.23:6379
    REDIS_PASSWORD=byt6gc1w0aed2q7h
    REDIS_DB=0
    
    SERVER_ADDR=:8080
    
    HUA_ZHANG_GUI_URL=https://api.shop.huahb.cn
    HUA_HUI_BAO_URL=https://api.shop.zhiguanhua.cn
    

要把对应配置项改为线上的

  1. 后台运行(简单方法) 使用 nohup&

    nohup ./main > ./message_server.log 2>&1 &
    

    查看进程:

    ps -ef | grep main