ComunicRTCProxy/ws.go

54 lines
848 B
Go

// Websocket controller
//
// @author Pierre HUBERT
package main
import (
"log"
"os"
"os/signal"
"github.com/gorilla/websocket"
)
// Open websocket connection
func openWs(conf *Config) {
// Auto-close connection when the system request it
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)
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()
// Wait for interrupt signal
go func() {
<-interrupt
c.Close()
}()
// 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)
}
}