Voice and text communications platform powered by WebRTC
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

72 lines
1.2 KiB

package web
import (
"log"
"net/http"
rice "github.com/GeertJohan/go.rice"
"github.com/gorilla/mux"
"github.com/gorilla/websocket"
)
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
return true
},
EnableCompression: true,
}
type WebInterface struct {
}
func NewWebInterface(address string) *WebInterface {
w := WebInterface{}
r := mux.NewRouter()
r.HandleFunc("/w", webSocketHandler)
r.PathPrefix("/").Handler(http.FileServer(rice.MustFindBox("public").HTTPBox()))
go func() {
if err := http.ListenAndServe(address, r); err != nil {
log.Fatal("Web server error: ", err)
}
}()
return &w
}
func webSocketHandler(w http.ResponseWriter, r *http.Request) {
log.Println("new WS")
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
return
}
vars := mux.Vars(r)
err = conn.WriteMessage(websocket.TextMessage, []byte("Test server write"))
if err != nil {
conn.Close()
return
}
_ = vars
var (
messageType int
message []byte
)
for {
messageType, message, err = conn.ReadMessage()
if err != nil {
conn.Close()
return
}
log.Printf("received %d %d", messageType, len(message))
_ = messageType
}
}