// Websocket controller // // @author Pierre HUBERT package main import ( "encoding/json" "log" "os" "os/signal" "github.com/gorilla/websocket" ) // WsMessage is a message from or to the api server type WsMessage struct { Title string CallID string PeerID string Data interface{} } // 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 } // Process incoming messages log.Printf("recv: %s", message) // Decode JSON var msg WsMessage json.Unmarshal(message, &msg) switch msg.Title { // call configuration case "config": println("Got call configuration") setCallConfig(msg.Data.(map[string]interface{})) break } } }