Exemple #1
0
func (r *RoomBase) broadcast(
	ctx scope.Context, cmdType proto.PacketType, payload interface{}, excluding ...proto.Session) error {

	excMap := make(map[string]struct{}, len(excluding))
	for _, x := range excluding {
		if x != nil {
			excMap[x.ID()] = struct{}{}
		}
	}

	for _, sessions := range r.live {
		for _, session := range sessions {
			if _, ok := excMap[session.ID()]; ok {
				continue
			}
			if err := session.Send(ctx, cmdType.Event(), payload); err != nil {
				// TODO: accumulate errors
				return err
			}
		}
	}

	if cmdType == proto.PartEventType {
		if presence, ok := payload.(*proto.PresenceEvent); ok {
			if waiter, ok := r.partWaiters[presence.SessionID]; ok {
				r.m.Unlock()
				waiter <- struct{}{}
				r.m.Lock()
			}
		}
	}
	return nil
}
Exemple #2
0
func (r *memRoom) broadcast(
	ctx scope.Context, cmdType proto.PacketType, payload interface{}, excluding ...proto.Session) error {

	excMap := make(map[string]struct{}, len(excluding))
	for _, x := range excluding {
		if x != nil {
			excMap[x.ID()] = struct{}{}
		}
	}

	for _, sessions := range r.live {
		for _, session := range sessions {
			if _, ok := excMap[session.ID()]; ok {
				continue
			}
			if err := session.Send(ctx, cmdType.Event(), payload); err != nil {
				// TODO: accumulate errors
				return err
			}
		}
	}
	return nil
}