Ver Fonte

添加websocket接口

yjp há 5 meses atrás
pai
commit
e293f4c6a9
1 ficheiros alterados com 15 adições e 0 exclusões
  1. 15 0
      websocket/websocket.go

+ 15 - 0
websocket/websocket.go

@@ -14,6 +14,7 @@ type HandleErrorFunc func(err error, context map[string]any)
 type HandleCloseFunc func(i int, s string, context map[string]any) error
 type HandlePongFunc func(context map[string]any)
 type HandleMessageFunc func(message []byte, context map[string]any)
+type BroadCastFilterCallback func(context map[string]any) bool
 
 var managerInstance *Manager
 
@@ -254,6 +255,20 @@ func (m *Manager) BroadCast(groupID string, msg []byte) error {
 	return melodyInstance.Broadcast(msg)
 }
 
+func (m *Manager) BroadCastFilter(groupID string, msg []byte, filterCallback BroadCastFilterCallback) error {
+	m.melodyMapMutex.RLock()
+	defer m.melodyMapMutex.RUnlock()
+
+	melodyInstance, ok := m.melodyMap[groupID]
+	if !ok {
+		return errors.New("groupID尚未注册")
+	}
+
+	return melodyInstance.BroadcastFilter(msg, func(session *melody.Session) bool {
+		return filterCallback(session.Keys)
+	})
+}
+
 type InitOption func(*InitOptions)
 
 type InitOptions struct {