id.go
changeset 126 367e76b3028e
parent 125 f464f14e39a7
child 127 a8f9a0c07fc8
equal deleted inserted replaced
125:f464f14e39a7 126:367e76b3028e
     1 package xmpp
       
     2 
       
     3 // Code to generate unique IDs for outgoing messages.
       
     4 
       
     5 import (
       
     6 	"fmt"
       
     7 )
       
     8 
       
     9 var id <-chan string
       
    10 
       
    11 func init() {
       
    12 	// Start the unique id generator.
       
    13 	idCh := make(chan string)
       
    14 	id = idCh
       
    15 	go func(ch chan<- string) {
       
    16 		id := int64(1)
       
    17 		for {
       
    18 			str := fmt.Sprintf("id_%d", id)
       
    19 			ch <- str
       
    20 			id++
       
    21 		}
       
    22 	}(idCh)
       
    23 }
       
    24 
       
    25 // This function may be used as a convenient way to generate a unique
       
    26 // id for an outgoing iq, message, or presence stanza.
       
    27 func NextId() string {
       
    28 	return <-id
       
    29 }