github.com/vmware/transport-go@v1.3.4/plank/cmd/broker_sample/app.go (about) 1 // Copyright 2019-2021 VMware, Inc. 2 // SPDX-License-Identifier: BSD-2-Clause 3 4 package main 5 6 import ( 7 "github.com/streadway/amqp" 8 "github.com/vmware/transport-go/plank/cmd/broker_sample/plank" 9 "github.com/vmware/transport-go/plank/cmd/broker_sample/rabbitmq" 10 "github.com/vmware/transport-go/plank/utils" 11 "os" 12 "os/signal" 13 "syscall" 14 "time" 15 ) 16 17 // main app to test drive transport's broker APIs. provide LISTEN_METHOD and PRODUCE_MESSAGE_ON_RABBITMQ as 18 // environment variables to control the behavior of this app. 19 // LISTEN_METHOD: decides how/where the demo app should receive messages from (plank_ws, plank_stomp, rbmq_amqp, rbmq_stomp) 20 // PRODUCE_MESSAGE_ON_RABBITMQ: when set to 1 it sends a dummy message every two seconds 21 // WS_USE_TLS: when set to 1, connection to Plank WebSocket will be made through wss protocol instead of ws 22 func main() { 23 listenMethod := os.Getenv("LISTEN_METHOD") 24 produceMessage := os.Getenv("PRODUCE_MESSAGE_ON_RABBITMQ") 25 wsUseTls := os.Getenv("WS_USE_TLS") == "1" 26 27 var ch *amqp.Channel 28 c := make(chan os.Signal) 29 signal.Notify(c, syscall.SIGINT, syscall.SIGKILL) 30 31 // if PRODUCE_MESSAGE_ON_RABBITMQ is set to 1 then randomly send a message to a sample topic every two seconds 32 if produceMessage == "1" { 33 go func() { 34 conn, err := rabbitmq.GetNewConnection("amqp://guest:guest@localhost:5672") 35 if err != nil { 36 utils.Log.Fatalln(err) 37 } 38 if ch, err = rabbitmq.GetNewChannel(conn); err != nil { 39 utils.Log.Fatalln(err) 40 } 41 defer ch.Close() 42 43 // send a message every two seconds 44 for { 45 time.Sleep(2 * time.Second) 46 if err = rabbitmq.SendTopic(ch); err != nil { 47 utils.Log.Errorln(err) 48 c <- syscall.SIGKILL 49 } 50 } 51 }() 52 } 53 54 switch listenMethod { 55 case "plank_ws": 56 // listen to the sample channel on Plank through WebSocket. 57 // need to have a Plank instance running at default port (30080) 58 plank.ListenViaWS(c, wsUseTls) 59 break 60 case "plank_stomp": 61 // listen to the sample channel on Plank through STOMP exposed at TCP port 61613 62 plank.ListenViaStomp(c) 63 case "rbmq_amqp": 64 // listen to the sample channel through AMQP 65 // need to have a RabbitMQ instance running at default port 66 rabbitmq.ListenViaAmqp(c) 67 break 68 case "rbmq_stomp": 69 // listen to the sample channel through STOMP 70 // need to have a RabbitMQ instance with STOMP plugin enabled (which listens at port 61613) 71 rabbitmq.ListenViaStomp(c) 72 } 73 }