GithubHelp home page GithubHelp logo

mqenergy / go-websocket Goto Github PK

View Code? Open in Web Editor NEW
20.0 2.0 2.0 93 KB

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

License: MIT License

Go 100.00%
go golang push websocket ws gorilla-websocket

go-websocket's Introduction

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": "客户端消息发送成功",
}

go-websocket's People

Contributors

chenxi2015 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

hjc1985 z1076

go-websocket's Issues

GetLocalIpToInt本地ip转负数导致panic异常问题

用的一个项目使用了这个包,启动的时候异常了,排查到问题是本地ip可能转换得到负数,除余1023仍然得到负数,然后snowflake约定初始值是0-1023导致panic项目直接启动不了。

// localIp得到负数情况下panic代码 
Node, err = snowflake.NewNode(int64(localIp) % 1023)
if err != nil {
     panic(err)
}

// 雪花算法抛出error逻辑
if n.node < 0 || n.node > n.nodeMax {
    return nil, errors.New("Node number must be between 0 and " + strconv.FormatInt(n.nodeMax, 10))
}

如果确认是负数引起的问题,考虑解决方案

  1. func GetLocalIpToInt() (uint32, error) 参考
  2. 直接在snowflake.NewNode入口处添加localIp转正数的逻辑

请教关于Hub Clients map

go的初学者,有个疑问,能否请教一下
type Hub struct { Clients map[*Client]bool ....

Hub 的Clients key是 *Client类型, value是个bool类型, 我看gorilla websocket chat example也是这样做的, 但是它是没有相关单客户推送的需求. 这是作者实现的单客户推送 是需要循环比对去取*Client,为什么不直接使用 map[int] *Client类型呢, key直接是snowflake产生的id, 还是经过权衡是原方式更有性能优势 ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.