增加websocket库
This commit is contained in:
39
aycache/aycache.go
Normal file
39
aycache/aycache.go
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
package aycache
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/ayflying/utility_go/pkg/aycache/drive"
|
||||||
|
drive2 "github.com/ayflying/utility_go/pkg/aycache/drive"
|
||||||
|
"github.com/gogf/gf/v2/os/gcache"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Mod struct {
|
||||||
|
client *gcache.Cache
|
||||||
|
}
|
||||||
|
|
||||||
|
//func NewV1(_name ...string) *cache.Mod {
|
||||||
|
// return pgk.Cache
|
||||||
|
//}
|
||||||
|
|
||||||
|
// Deprecated:弃用,改用 pgk.Cache()
|
||||||
|
func New(_name ...string) gcache.Adapter {
|
||||||
|
|
||||||
|
var cacheAdapterObj gcache.Adapter
|
||||||
|
var name = "cache"
|
||||||
|
if len(_name) > 0 {
|
||||||
|
name = _name[0]
|
||||||
|
}
|
||||||
|
switch name {
|
||||||
|
case "cache":
|
||||||
|
cacheAdapterObj = drive2.NewAdapterMemory()
|
||||||
|
case "redis":
|
||||||
|
cacheAdapterObj = drive2.NewAdapterRedis()
|
||||||
|
case "file":
|
||||||
|
cacheAdapterObj = drive2.NewAdapterFile("runtime/cache")
|
||||||
|
case "es":
|
||||||
|
cacheAdapterObj = drive.NewAdapterElasticsearch([]string{"http://127.0.0.1:9200"})
|
||||||
|
}
|
||||||
|
|
||||||
|
//var client = gcache.New()
|
||||||
|
//client.SetAdapter(cacheAdapterObj)
|
||||||
|
return cacheAdapterObj
|
||||||
|
}
|
||||||
@@ -1,7 +1,9 @@
|
|||||||
package websocket
|
package websocket
|
||||||
|
|
||||||
|
import "context"
|
||||||
|
|
||||||
// 定义一个处理方法的类型
|
// 定义一个处理方法的类型
|
||||||
type Handler func(uid int64, data []byte)
|
type Handler func(ctx context.Context, req any) (err error)
|
||||||
|
|
||||||
// 路由器的处理映射
|
// 路由器的处理映射
|
||||||
var (
|
var (
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package websocket
|
package websocket
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"github.com/gogf/gf/v2/container/gmap"
|
"github.com/gogf/gf/v2/container/gmap"
|
||||||
"github.com/gogf/gf/v2/frame/g"
|
"github.com/gogf/gf/v2/frame/g"
|
||||||
"github.com/gogf/gf/v2/os/gctx"
|
"github.com/gogf/gf/v2/os/gctx"
|
||||||
@@ -15,7 +16,7 @@ import (
|
|||||||
type SocketV1 struct{}
|
type SocketV1 struct{}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ctx = gctx.New()
|
//ctx = gctx.New()
|
||||||
//Conn map[uuid.UUID]*WebsocketData
|
//Conn map[uuid.UUID]*WebsocketData
|
||||||
lock sync.Mutex
|
lock sync.Mutex
|
||||||
|
|
||||||
@@ -26,7 +27,7 @@ type WebsocketData struct {
|
|||||||
Ws *websocket.Conn
|
Ws *websocket.Conn
|
||||||
Uuid uuid.UUID
|
Uuid uuid.UUID
|
||||||
Uid int64
|
Uid int64
|
||||||
Data g.Var
|
Ctx context.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewV1() *SocketV1 {
|
func NewV1() *SocketV1 {
|
||||||
@@ -51,6 +52,7 @@ func (s *SocketV1) Load(serv *ghttp.Server, prefix string) {
|
|||||||
}
|
}
|
||||||
group.Bind(
|
group.Bind(
|
||||||
func(r *ghttp.Request) {
|
func(r *ghttp.Request) {
|
||||||
|
ctx := r.Context()
|
||||||
ws, err := websocketCfg.Upgrade(r.Response.Writer, r.Request, nil)
|
ws, err := websocketCfg.Upgrade(r.Response.Writer, r.Request, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
glog.Error(ctx, err)
|
glog.Error(ctx, err)
|
||||||
@@ -58,7 +60,7 @@ func (s *SocketV1) Load(serv *ghttp.Server, prefix string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//ws联机触发器
|
//ws联机触发器
|
||||||
NewV1().OnConnect(ws)
|
NewV1().OnConnect(ctx, ws)
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -70,7 +72,7 @@ func (s *SocketV1) Load(serv *ghttp.Server, prefix string) {
|
|||||||
// @Description:
|
// @Description:
|
||||||
// @receiver s
|
// @receiver s
|
||||||
// @param conn
|
// @param conn
|
||||||
func (s *SocketV1) OnConnect(conn *websocket.Conn) {
|
func (s *SocketV1) OnConnect(ctx context.Context, conn *websocket.Conn) {
|
||||||
//lock.Lock()
|
//lock.Lock()
|
||||||
//defer lock.Unlock()
|
//defer lock.Unlock()
|
||||||
|
|
||||||
@@ -81,7 +83,7 @@ func (s *SocketV1) OnConnect(conn *websocket.Conn) {
|
|||||||
data := &WebsocketData{
|
data := &WebsocketData{
|
||||||
Uuid: id,
|
Uuid: id,
|
||||||
Ws: conn,
|
Ws: conn,
|
||||||
Data: g.Var{},
|
Ctx: ctx,
|
||||||
}
|
}
|
||||||
m.Set(id, data)
|
m.Set(id, data)
|
||||||
|
|
||||||
@@ -121,7 +123,7 @@ func (s *SocketV1) OnMessage(conn *WebsocketData, req []byte, msgType int) {
|
|||||||
handler, exist := handlers[cmd]
|
handler, exist := handlers[cmd]
|
||||||
if exist {
|
if exist {
|
||||||
//匹配上路由器
|
//匹配上路由器
|
||||||
handler(conn.Uid, []byte(msg))
|
handler(conn.Ctx, msg)
|
||||||
} else {
|
} else {
|
||||||
//fmt.Println("未注册的路由器ID:", cmd)
|
//fmt.Println("未注册的路由器ID:", cmd)
|
||||||
s.Send(conn.Uuid, []byte("未注册的协议号:"+msgStr[:8]))
|
s.Send(conn.Uuid, []byte("未注册的协议号:"+msgStr[:8]))
|
||||||
@@ -166,7 +168,7 @@ func (s *SocketV1) SendAll(data []byte) {
|
|||||||
// @param conn
|
// @param conn
|
||||||
func (s *SocketV1) OnClose(id uuid.UUID, conn *websocket.Conn) {
|
func (s *SocketV1) OnClose(id uuid.UUID, conn *websocket.Conn) {
|
||||||
// 在此处编写断开连接后的处理逻辑
|
// 在此处编写断开连接后的处理逻辑
|
||||||
g.Log().Debugf(ctx, "WebSocket connection from %s has been closed.", conn.RemoteAddr())
|
g.Log().Debugf(gctx.New(), "WebSocket connection from %s has been closed.", conn.RemoteAddr())
|
||||||
|
|
||||||
// 可能的后续操作:
|
// 可能的后续操作:
|
||||||
// 1. 更新连接状态或从连接池移除
|
// 1. 更新连接状态或从连接池移除
|
||||||
|
|||||||
Reference in New Issue
Block a user