Esempio n. 1
0
// subscribe will add the given protocol client to the channel to subscribe to
func (b *PubSubBackend) subscribe(data interface{}, client server.ProtocolClient) error {
	d, _ := data.([][]byte)
	if len(d) < 1 {
		return nil
	} else {
		for _, k := range d {
			key := string(k)
			if topic, ok := b.topics[key]; ok {
				topic.Lock()
				id := client.Address()
				if _, ok = topic.clients[id]; !ok {
					topic.clients[id] = empty
					topic.size++
				}
				topic.Unlock()
			} else {
				topic := new(TopicChannel)
				topic.clients = make(map[string]struct{})
				topic.size = 1
				topic.clients[client.Address()] = empty
				b.topics[key] = topic
			}
		}

		return nil
	}
}
Esempio n. 2
0
func (b *PubSubBackend) unsubscribe(data interface{}, client server.ProtocolClient) error {
	d, _ := data.([][]byte)
	if len(d) < 1 {
		return nil
	} else {
		for _, k := range d {
			key := string(k)
			if topic, ok := b.topics[key]; ok {
				topic.Lock()
				id := client.Address()
				if _, ok = topic.clients[id]; ok {
					delete(topic.clients, id)
					topic.size--
				}
				topic.Unlock()
			}
		}

		return nil
	}
}