1
0
mirror of https://github.com/amitbet/vncproxy.git synced 2025-05-04 05:36:21 +00:00
vncproxy/server/ws-server-go.go
2017-07-20 02:01:40 +03:00

46 lines
802 B
Go

package server
import (
"io"
"net/http"
"net/url"
"vncproxy/logger"
"golang.org/x/net/websocket"
)
type WsServer struct {
cfg *ServerConfig
}
type WsHandler func(io.ReadWriter, *ServerConfig, string)
func (wsServer *WsServer) Listen(urlStr string, handlerFunc WsHandler) {
if urlStr == "" {
urlStr = "/"
}
url, err := url.Parse(urlStr)
if err != nil {
logger.Errorf("error while parsing url: ", err)
}
http.Handle(url.Path, websocket.Handler(
func(ws *websocket.Conn) {
path := ws.Request().URL.Path
var sessionId string
if path != "" {
sessionId = path[1:]
}
ws.PayloadType = websocket.BinaryFrame
handlerFunc(ws, wsServer.cfg, sessionId)
}))
err = http.ListenAndServe(url.Host, nil)
if err != nil {
panic("ListenAndServe: " + err.Error())
}
}