示例#1
0
func LoopMessage() {
	for {
		var msg Message
		msg = Message{
			ID:   util.RandomID(),
			Addr: self,
			Body: "進捗どうですか?",
		}
		Broadcast(msg)
		time.Sleep(200 * time.Millisecond)
		msg = Message{
			ID:   util.RandomID(),
			Addr: self,
			Body: "進捗ダメです",
		}
		Broadcast(msg)
		time.Sleep(200 * time.Millisecond)
	}
}
示例#2
0
func Broadcast(msg Message) {
	if *wildFlag {
		msg.ID = util.RandomID()
	}
	for _, ch := range peers.List() {
		select {
		case ch <- msg: // Send message into a channel
		default: // Drop message
		}
	}
}
示例#3
0
文件: part8.go 项目: smly/sandbox
func ReadInput() {
	scanner := bufio.NewScanner(os.Stdin)
	for scanner.Scan() {
		msg := Message{
			ID:   util.RandomID(),
			Addr: self,
			Body: scanner.Text(),
		}
		Broadcast(msg)
	}
	if err := scanner.Err(); err != nil {
		log.Fatal(err)
	}
}
示例#4
0
func readInput() {
	s := bufio.NewScanner(os.Stdin)
	for s.Scan() {
		m := Message{
			ID:   util.RandomID(),
			Addr: self,
			Body: s.Text(),
		}
		Seen(m.ID)
		broadcast(m)
	}
	if err := s.Err(); err != nil {
		log.Fatal(err)
	}
}
示例#5
0
func readInput() {
	r := bufio.NewReader(os.Stdin)
	for {
		s, err := r.ReadString('\n')
		if err != nil {
			log.Fatal(err)
		}
		m := Message{
			ID:   util.RandomID(),
			Addr: self,
			Body: s[:len(s)-1],
		}
		Seen(m.ID)
		broadcast(m)
	}
}
示例#6
0
func (l *Logger) WriteTo(w io.Writer) error {
	id := util.RandomID()
	ch := make(chan []byte)
	l.mu.Lock()
	l.m[id] = ch
	l.mu.Unlock()
	defer func() {
		l.mu.Lock()
		delete(l.m, id)
		l.mu.Unlock()
	}()
	for b := range ch {
		_, err := w.Write(b)
		if err != nil {
			return err
		}
	}
	return nil
}
示例#7
0
// Read the information we got from our TCP connection
func readInput() {
	// Create a new scanner
	s := bufio.NewScanner(os.Stdin)

	// While we are scanning the message
	for s.Scan() {
		// Initialize our message struct, getting the information out of it
		m := Message{
			ID:   util.RandomID(),
			Addr: self,
			Body: s.Text(),
		}
		// Add the ID of the message to a list of seen peers
		Seen(m.ID)

		// Broadcast the message out to all of the other peers
		broadcast(m)
	}
	if err := s.Err(); err != nil {
		log.Fatal(err)
	}
}
示例#8
0
func main() {
	id := util.RandomID()
	fmt.Println(id)
}