mirror of
https://github.com/amitbet/vncproxy.git
synced 2025-09-21 17:39:28 +00:00
Added server for web sockets, plus some refactoring
This commit is contained in:
43
server/ws-server-go.go
Normal file
43
server/ws-server-go.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"golang.org/x/net/websocket"
|
||||
)
|
||||
|
||||
type WsServer struct {
|
||||
cfg *ServerConfig
|
||||
}
|
||||
|
||||
type WsHandler func(io.ReadWriter, *ServerConfig)
|
||||
|
||||
// This example demonstrates a trivial echo server.
|
||||
func (wsServer *WsServer) Listen(urlStr string, handlerFunc WsHandler) {
|
||||
//http.Handle("/", websocket.Handler(EchoHandler))
|
||||
if urlStr == "" {
|
||||
urlStr = "/"
|
||||
}
|
||||
url, err := url.Parse(urlStr)
|
||||
if err != nil {
|
||||
fmt.Println("error while parsing url: ", err)
|
||||
}
|
||||
|
||||
http.Handle(url.Path, websocket.Handler(func(ws *websocket.Conn) {
|
||||
// header := ws.Request().Header
|
||||
// url := ws.Request().URL
|
||||
// //stam := header.Get("Origin")
|
||||
// fmt.Printf("header: %v\nurl: %v\n", header, url)
|
||||
// io.Copy(ws, ws)
|
||||
ws.PayloadType = websocket.BinaryFrame
|
||||
handlerFunc(ws, wsServer.cfg)
|
||||
}))
|
||||
|
||||
err = http.ListenAndServe(url.Host, nil)
|
||||
if err != nil {
|
||||
panic("ListenAndServe: " + err.Error())
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user