Example #1
0
func bot() func() {
	conn, err := grpc.Dial(":5000")
	if err != nil {
		log.Fatalln("net.Dial:", err)
	}
	client := pb.NewChatClient(conn)

	sid, err := common.Authorize(client, "bot")
	if err != nil {
		log.Fatalln("authorize:", err)
	}

	events, err := common.Connect(client, sid)
	if err != nil {
		log.Fatalln("connect:", err)
	}

	return func() {
		defer conn.Close()
		interval := float64(time.Second) / *m
		time.Sleep(time.Duration(interval * rand.Float64()))
		tick := time.Tick(time.Duration(interval))
		for {
			select {
			case <-tick:
				err := common.Say(client, sid, "foobarbazfoobarbaz")
				if err != nil {
					log.Fatalln("say:", err)
				}
			case <-events:
			}
		}
	}
}
Example #2
0
func main() {
	conn, err := grpc.Dial(":5000")
	if err != nil {
		log.Fatalln("net.Dial:", err)
	}
	defer conn.Close()
	client := pb.NewChatClient(conn)

	var name string
	for {
		fmt.Print("name> ")
		if n, err := fmt.Scanln(&name); err == io.EOF {
			return
		} else if n == 0 {
			fmt.Println("name must be not empty")
			continue
		} else if n > 20 {
			fmt.Println("name must be less than or equal 20 characters")
			continue
		}
		break
	}

	sid, err := common.Authorize(client, name)
	if err != nil {
		log.Fatalln("authorize:", err)
	}

	events, err := common.Connect(client, sid)
	if err != nil {
		log.Fatalln("connect:", err)
	}

	go func() {
		for {
			select {
			case event := <-events:
				switch {
				case event.Join != nil:
					fmt.Printf("%s has joined.\n", event.Join.Name)
				case event.Leave != nil:
					fmt.Printf("%s has left.\n", event.Leave.Name)
				case event.Log != nil:
					fmt.Printf("%s> %s\n", event.Log.Name, event.Log.Message)
				}
			}
		}
	}()

	var message string
	for {
		fmt.Print("> ")
		if n, err := fmt.Scanln(&message); err == io.EOF {
			return
		} else if n > 0 {
			err := common.Say(client, sid, message)
			if err != nil {
				log.Fatalln("say:", err)
			}
		}
	}
}