2020-04-10 07:43:14 +00:00
|
|
|
// Websocket controller
|
|
|
|
//
|
|
|
|
// @author Pierre HUBERT
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
2020-04-10 08:14:16 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2020-04-10 07:43:14 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Open websocket connection
|
|
|
|
func openWs(conf *Config) {
|
|
|
|
|
2020-04-10 08:14:16 +00:00
|
|
|
// Auto-close connection when the system request it
|
|
|
|
interrupt := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(interrupt, os.Interrupt)
|
|
|
|
|
2020-04-10 07:43:14 +00:00
|
|
|
u := conf.getURL()
|
|
|
|
log.Printf("Connecting to %s", u.String())
|
|
|
|
|
|
|
|
// Connect to Websocket
|
|
|
|
c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("dial:", err)
|
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
|
2020-04-10 08:14:16 +00:00
|
|
|
// Wait for interrupt signal
|
|
|
|
go func() {
|
|
|
|
<-interrupt
|
|
|
|
c.Close()
|
|
|
|
}()
|
|
|
|
|
2020-04-10 07:43:14 +00:00
|
|
|
// Read remote messages
|
|
|
|
for {
|
|
|
|
|
|
|
|
_, message, err := c.ReadMessage()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("WS Read error: %s", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO : process incoming messages
|
|
|
|
log.Printf("recv: %s", message)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|