mirror of
https://gitlab.com/comunic/ComunicRTCProxy
synced 2024-11-17 02:51:12 +00:00
54 lines
848 B
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)
|
|
|
|
}
|
|
|
|
}
|