Example #1
0
func (c *conn) writeLoop() {
	ticker := time.NewTicker(pingTime)

	subscriber, err := broker.Subscribe(c.topic, func(msg *broker.Message) {
		b, err := json.Marshal(msg)
		if err != nil {
			return
		}
		c.write(websocket.TextMessage, b)
	})

	defer func() {
		subscriber.Unsubscribe()
		ticker.Stop()
		c.ws.Close()
	}()

	if err != nil {
		log.Error(err.Error())
		return
	}

	for _ = range ticker.C {
		if err := c.write(websocket.PingMessage, []byte{}); err != nil {
			return
		}
	}
}
Example #2
0
func sub() {
	_, err := broker.Subscribe(topic, func(msg *broker.Message) {
		fmt.Println("[sub] received message:", string(msg.Body), "header", msg.Header)
	})
	if err != nil {
		fmt.Println(err)
	}
}