用 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 自行测试~
- 通用的消息推送架构设计图
欢迎各位大佬来玩💖 直通车
本作品采用《CC 协议》,转载必须注明作者和本文链接