Categorygithub.com/MQEnergy/go-websocket
modulepackage
0.0.0-20240523002409-854417ba53b7
Repository: https://github.com/mqenergy/go-websocket.git
Documentation: pkg.go.dev

# README

go-websocket

基于gorilla/websocket封装的websocket库,实现基于系统维度的消息推送,基于群组维度的消息推送,基于单个和多个客户端消息推送。

GoDoc Go Report Card codebeat badge GitHub license

一、目录结构

├── LICENSE
├── README.md
├── client.go           // 客户端
├── client_hub.go       // 客户端集线器
├── code.go             // 状态码
├── example             // 案例
│   └── ws.go
├── go.mod
├── go.sum
├── log.go              // 日志
├── node.go             // 节点(用于在分布式系统生成基于节点的客户端连接ID)
├── response.go         // 客户端发送消息
└── server.go           // 服务

二、在项目中安装使用

go get -u github.com/MQEnergy/go-websocket

三、运行example

1、开启服务

go run examples/ws.go
服务器启动成功,端口号 :9991 

代表启动成功

2、案例

具体查看example目录

1)连接ws并加群组

system_id为系统ID(不必填 不填默认当前节点ip的int值) group_id为群组ID(不必填 不填连接不加群组 注意:群组id为全局唯一ID 不然可能会出现不同系统的相同群组都推送消息)

请求

ws://127.0.0.1:9991/ws?system_id=123&group_id=test

可选多种返回方式 如: Text,Json,Binary(二进制方式) 返回如下json示例:

{
    "code": 0,
    "msg": "客户端连接成功",
    "data": {
        "client_id": "1589962851152388096",
        "group_id": "test",
        "system_id": "123"
    },
    "params": null
}

2)全局广播消息群发

请求

http://127.0.0.1:9991/push_to_system?system_id=123&data={"hello":"world"}

返回

{
    "msg": "系统消息发送成功",
}

3)单个系统消息群发

请求

http://127.0.0.1:9991/push_to_system?system_id=123&data={"hello":"world"}

返回

{
    "msg": "系统消息发送成功",
}

4)推送消息到群组

请求

http://127.0.0.1:9991/push_to_group?system_id=123&group_id=test&data={"hello":"world1"}

返回

{
    "msg": "群组消息发送成功",
}

5)单个客户端消息发送

请求

http://127.0.0.1:9991/push_to_client?client_id=123&data={"hello":"world"}

返回

{
    "msg": "客户端消息发送成功",
}

# Packages

No description provided by the author

# Functions

GenerateUuid 生成唯一ID.
GetLocalIpToInt 获取本机IP转成int.
NewHub 实例化.
TraceClientCloseFailedLog 客户端关闭失败消息.
TraceClientCloseSuccessLog 客户端关闭成功消息.
TraceHeartbeatErrdLog 心跳检测失败消息.
TraceLog 写日志.
TraceReadMsgSuccessLog 读取消息体成功消息.
TraceSendMsgErrLog 发送消息体失败.
TraceSuccessLog 客户端连接成功消息.
WriteBindGroupSuccessJson 返回绑定群组成功.
WriteClientCloseFailedJson 返回客户端关闭失败.
WriteClientCloseSuccessJson 返回客户端关闭成功.
WriteClientFailedJson 返回客户端主动断连.
WriteClientNotExistJson 返回客户端不存在.
WriteFailedJson 返回客户端连接失败.
WriteHeartbeatErrJson 返回心跳检测失败.
WriteJson 返回给客户端的信息.
WriteMessage 返回给客户端的信息.
WriteReadMsgErrJson 返回读取消息体失败.
WriteReadMsgSuccessJson 返回读取消息体成功.
WriteRequestParamErrJson 返回请求参数错误.
WriteSendMsgErrJson 返回发送消息体失败.
WriteSendMsgSuccessJson 返回发送消息体成功.
WriteSuccessJson 返回客户端连接成功.
WsServer 处理websocket请求.

# Constants

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Variables

No description provided by the author
No description provided by the author
No description provided by the author

# Structs

No description provided by the author
No description provided by the author
No description provided by the author

# Type aliases

No description provided by the author
No description provided by the author