用 golang 去实现类似 swoole 的 websocket 服务 😂

gws 为一个基于事件与切面编程思想所实现的一个基础框架

  • 项目灵感来源 swoole 。
  • 感谢 github.com/gorilla/websocket 为本项目提供 webSocket 服务所需的基础。

怎么使用?

  • 我使用 go mod 作为包管理工具
  • go.mod 中 加入 github.com/whyiyhw/gws 或者 go get github.com/whyiyhw/gws
    // default 127.0.0.1:9501/ws
    s := new(gws.Server)

    // 接收消息事件
    s.OnMessage = func(c *gws.Conn, fd int, msg string, err error) {
        fmt.Printf("client %d said %s \n", fd, message)
    }

    // 连接成功事件
    s.OnOpen = func(c *gws.Conn, fd int) {
        fmt.Printf("client %d online \n", fd)
    }

    // 连接关闭事件
    s.OnClose = func(c *gws.Conn, fd int) {
        fmt.Printf("client %d had offline \n", fd)
    }

    // 启动服务
    if err := s.ListenAndServe(); err != nil {
        panic(err)
    }
  • 再使用 浏览器工具栏 连接 ws://127.0.0.1:9501/ws 就可以愉快的玩耍了~

其它 特性请点击查看 examples 自行测试~

  • 通用的消息推送架构设计图
    websocket

欢迎各位大佬来玩💖 直通车

本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!