代码拉取完成,页面将自动刷新
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)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。