|
| 1 | +# broadcast |
| 2 | + |
| 3 | +A demo of broadcasting a message from one node to several nodes providing the service named `'chat'`. |
| 4 | + |
| 5 | +``` |
| 6 | +[terminal 1] $ go run main.go -l :9000 |
| 7 | +2020/06/27 02:37:52 Listening for Flatend nodes on '[::]:9000'. |
| 8 | +2020/06/27 02:37:53 0.0.0.0:9001 has connected. Services: [chat] |
| 9 | +2020/06/27 02:37:56 0.0.0.0:9002 has connected. Services: [chat] |
| 10 | +hello |
| 11 | +Got 'world' from 0.0.0.0:9001! |
| 12 | +Got 'test' from 0.0.0.0:9002! |
| 13 | +2020/06/27 02:39:09 0.0.0.0:9002 has disconnected from you. Services: [chat] |
| 14 | +2020/06/27 02:39:10 0.0.0.0:9001 has disconnected from you. Services: [chat] |
| 15 | +
|
| 16 | +[terminal 2] $ go run main.go -l :9001 :9000 |
| 17 | +2020/06/27 02:37:53 Listening for Flatend nodes on '[::]:9001'. |
| 18 | +2020/06/27 02:37:53 You are now connected to 0.0.0.0:9000. Services: [chat] |
| 19 | +2020/06/27 02:37:53 Re-probed 0.0.0.0:9000. Services: [chat] |
| 20 | +2020/06/27 02:37:53 Discovered 0 peer(s). |
| 21 | +2020/06/27 02:37:56 0.0.0.0:9002 has connected. Services: [chat] |
| 22 | +Got 'hello' from 0.0.0.0:9000! |
| 23 | +world |
| 24 | +Got 'test' from 0.0.0.0:9002! |
| 25 | +2020/06/27 02:39:09 0.0.0.0:9002 has disconnected from you. Services: [chat] |
| 26 | +
|
| 27 | +[terminal 3] $ go run main.go -l :9002 :9000 |
| 28 | +2020/06/27 02:37:56 Listening for Flatend nodes on '[::]:9002'. |
| 29 | +2020/06/27 02:37:56 You are now connected to 0.0.0.0:9000. Services: [chat] |
| 30 | +2020/06/27 02:37:56 Re-probed 0.0.0.0:9000. Services: [chat] |
| 31 | +2020/06/27 02:37:56 You are now connected to 0.0.0.0:9001. Services: [chat] |
| 32 | +2020/06/27 02:37:56 Discovered 1 peer(s). |
| 33 | +Got 'hello' from 0.0.0.0:9000! |
| 34 | +Got 'world' from 0.0.0.0:9001! |
| 35 | +``` |
| 36 | + |
| 37 | +```go |
| 38 | +package main |
| 39 | + |
| 40 | +import ( |
| 41 | + "bufio" |
| 42 | + "bytes" |
| 43 | + "flag" |
| 44 | + "fmt" |
| 45 | + "github.com/lithdew/flatend" |
| 46 | + "io/ioutil" |
| 47 | + "os" |
| 48 | +) |
| 49 | + |
| 50 | +func check(err error) { |
| 51 | + if err != nil { |
| 52 | + panic(err) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func main() { |
| 57 | + var listenAddr string |
| 58 | + flag.StringVar(&listenAddr, "l", ":9000", "address to listen for peers on") |
| 59 | + flag.Parse() |
| 60 | + |
| 61 | + node := &flatend.Node{ |
| 62 | + PublicAddr: listenAddr, |
| 63 | + BindAddrs: []string{listenAddr}, |
| 64 | + SecretKey: flatend.GenerateSecretKey(), |
| 65 | + Services: map[string]flatend.Handler{ |
| 66 | + "chat": func(ctx *flatend.Context) { |
| 67 | + buf, err := ioutil.ReadAll(ctx.Body) |
| 68 | + if err != nil { |
| 69 | + return |
| 70 | + } |
| 71 | + fmt.Printf("Got '%s' from %s:%d!\n", string(buf), ctx.ID.Host.String(), ctx.ID.Port) |
| 72 | + }, |
| 73 | + }, |
| 74 | + } |
| 75 | + defer node.Shutdown() |
| 76 | + |
| 77 | + check(node.Start(flag.Args()...)) |
| 78 | + |
| 79 | + br := bufio.NewReader(os.Stdin) |
| 80 | + for { |
| 81 | + line, _, err := br.ReadLine() |
| 82 | + if err != nil { |
| 83 | + break |
| 84 | + } |
| 85 | + |
| 86 | + line = bytes.TrimSpace(line) |
| 87 | + if len(line) == 0 { |
| 88 | + continue |
| 89 | + } |
| 90 | + |
| 91 | + providers := node.ProvidersFor("chat") |
| 92 | + for _, provider := range providers { |
| 93 | + _, err := provider.Push([]string{"chat"}, nil, ioutil.NopCloser(bytes.NewReader(line))) |
| 94 | + if err != nil { |
| 95 | + fmt.Printf("Unable to broadcast to %s: %s\n", provider.Addr(), err) |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | +``` |
0 commit comments