// For UNSUBSCRIBE message, we should remove the subscriber, and send back UNSUBACK func (this *service) processUnsubscribe(msg *message.UnsubscribeMessage) error { topics := msg.Topics() for _, t := range topics { this.topicsMgr.Unsubscribe(t, &this.onpub) this.sess.RemoveTopic(string(t)) } resp := message.NewUnsubackMessage() resp.SetPacketId(msg.PacketId()) _, err := this.writeMessage(resp) return err }
func (this *service) unsubscribe(msg *message.UnsubscribeMessage, onComplete OnCompleteFunc) error { _, err := this.writeMessage(msg) if err != nil { return fmt.Errorf("(%s) Error sending %s message: %v", this.cid(), msg.Name(), err) } var onc OnCompleteFunc = func(msg, ack message.Message, err error) error { onComplete := onComplete if err != nil { if onComplete != nil { return onComplete(msg, ack, err) } return err } unsub, ok := msg.(*message.UnsubscribeMessage) if !ok { if onComplete != nil { return onComplete(msg, ack, fmt.Errorf("Invalid UnsubscribeMessage received")) } return nil } unsuback, ok := ack.(*message.UnsubackMessage) if !ok { if onComplete != nil { return onComplete(msg, ack, fmt.Errorf("Invalid UnsubackMessage received")) } return nil } if unsub.PacketId() != unsuback.PacketId() { if onComplete != nil { return onComplete(msg, ack, fmt.Errorf("Unsub and Unsuback packet ID not the same. %d != %d.", unsub.PacketId(), unsuback.PacketId())) } return nil } var err2 error = nil for _, tb := range unsub.Topics() { // Remove all subscribers, which basically it's just this client, since // each client has it's own topic tree. err := this.topicsMgr.Unsubscribe(tb, nil) if err != nil { err2 = fmt.Errorf("%v\n%v", err2, err) } this.sess.RemoveTopic(string(tb)) } if onComplete != nil { return onComplete(msg, ack, err2) } return err2 } return this.sess.Unsuback.Wait(msg, onc) }