mirror of
https://github.com/amitbet/vncproxy.git
synced 2025-04-28 03:00:08 +00:00
46 lines
821 B
Go
46 lines
821 B
Go
package server
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
"github.com/amitbet/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())
|
|
}
|
|
}
|