yjp преди 5 месеца
родител
ревизия
e293f4c6a9
променени са 1 файла, в които са добавени 15 реда и са изтрити 0 реда
  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 {