1 Star 1 Fork 2

kristas / booting-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
websocket_handler.go 1.22 KB
一键复制 编辑 原始数据 按行查看 历史
kristas 提交于 2021-10-22 22:58 . feat: add annotation
package api
import (
"gitee.com/kristas/booting-go/framework"
"gitee.com/kristas/booting-go/framework/common/util/snowflake"
"gitee.com/kristas/booting-go/framework/core/log"
"gitee.com/kristas/booting-go/framework/core/statement/types"
. "gitee.com/kristas/booting-go/plugin/websocket_plugin"
"github.com/gorilla/websocket"
"strconv"
)
func init() {
framework.Component(new(WsHandler))
}
type WsHandler struct {
types.Websocket `endpoint:"/ws/channel/v1"`
Log log.Logger `wire:""`
}
func (w *WsHandler) OnOpen(session *Session) {
worker, _ := snowflake.NewWorker(1)
id := strconv.Itoa(int(worker.GetId()))
session.Set("id", id)
w.Log.Info("websocket[%s][connected]", id)
}
func (w *WsHandler) OnClose(session *Session) {
id, _ := session.Get("id")
w.Log.Info("websocket[%s][closed]", id)
}
func (w *WsHandler) OnMessage(session *Session, message []byte) error {
id, _ := session.Get("id")
w.Log.Info("websocket[%s][message]: %s", id, message)
go func() {
<-session.Context.Done()
w.Log.Info("done")
}()
return session.WriteMessage(websocket.TextMessage, message)
}
func (w *WsHandler) OnError(session *Session, err error) {
id, _ := session.Get("id")
w.Log.Error("websocket[%s][error]: %s", id, err)
}
Go
1
https://gitee.com/kristas/booting-go.git
git@gitee.com:kristas/booting-go.git
kristas
booting-go
booting-go
v1.4.4

搜索帮助