// AddListener adds a new listener for the topic. Returns a listener ID or a non-nil error.
// The callback functions for this DTopic are run by parallel.
AddListener adds a new listener for the topic. Returns a listener ID or a non-nil error.
The callback functions for this DTopic are run by parallel.
// AddListener adds a new listener for the topic . Returns a listener ID or a non - nil error . // The callback functions for this DTopic are run by parallel .
func (dt *DTopic) AddListener(f func(olric.DTopicMessage)) (uint64, error) {
	l := newListener()
	streamID, listenerID, err := dt.addStreamListener(l)
	if err != nil {
		return 0, err
	}
	req := protocol.NewDTopicMessage(protocol.OpDTopicAddListener)
	req.SetDTopic(dt.name)
	req.SetExtra(protocol.DTopicAddListenerExtra{
		ListenerID: listenerID,
		StreamID:   streamID,
	})
	resp, err := dt.request(req)
	if err != nil {
		_ = dt.removeStreamListener(listenerID)
		return 0, err
	}
	err = checkStatusCode(resp)
	if err != nil {
		_ = dt.removeStreamListener(listenerID)
		return 0, err
	}
	dt.wg.Add(1)
	go dt.listen(l, f)
	dt.mu.Lock()
	dt.listeners[listenerID] = struct{}{}
	dt.mu.Unlock()
	return listenerID, nil
}
func ( dt * DTopic ) AddListener ( f func ( olric . DTopicMessage ) ) ( uint64 , error ) { l := newListener ( ) streamID , listenerID , err := dt . addStreamListener ( l ) if err != nil { return 0 , err } req := protocol . NewDTopicMessage ( protocol . OpDTopicAddListener ) req . SetDTopic ( dt . name ) req . SetExtra ( protocol . DTopicAddListenerExtra { ListenerID : listenerID , StreamID : streamID , } ) resp , err := dt . request ( req ) if err != nil { _ = dt . removeStreamListener ( listenerID ) return 0 , err } err = checkStatusCode ( resp ) if err != nil { _ = dt . removeStreamListener ( listenerID ) return 0 , err } dt . wg . Add ( 1 ) go dt . listen ( l , f ) dt . mu . Lock ( ) dt . listeners [ listenerID ] = struct { } { } dt . mu . Unlock ( ) return listenerID , nil }
AddListener adds a new listener for the topic.
AddListener adds a new listener for the topic .
// Listen at background. This will call callback function when a message is received. // This DTopic needs to know its listeners. When the user calls Destroy method, we will call RemoveListener // for all of this ids.