feature: Websocket early impl
This commit is contained in:
parent
d74734972b
commit
f7fd8af124
|
|
@ -14,6 +14,7 @@ import (
|
|||
"net/http"
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// StartSingleHTTPFrontend will create a reverse proxy that proxies the API
|
||||
|
|
@ -37,8 +38,12 @@ func StartSingleHTTPFrontend(cfg *config.Config) {
|
|||
})
|
||||
|
||||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
log.Debugf("ui req: %q", r.URL)
|
||||
webuiProxy.ServeHTTP(w, r)
|
||||
if strings.Contains(r.Header.Get("Connection"), "Upgrade") {
|
||||
handleWebsocket(w, r)
|
||||
} else {
|
||||
log.Debugf("ui req: %q", r.URL)
|
||||
webuiProxy.ServeHTTP(w, r)
|
||||
}
|
||||
})
|
||||
|
||||
srv := &http.Server{
|
||||
|
|
|
|||
|
|
@ -5,14 +5,16 @@ import (
|
|||
log "github.com/sirupsen/logrus"
|
||||
"net/http"
|
||||
"nhooyr.io/websocket"
|
||||
"nhooyr.io/websocket/wsjson"
|
||||
"context"
|
||||
)
|
||||
|
||||
func handleWebsocket(w http.ResponseWriter, r *http.Request) {
|
||||
func handleWebsocket(w http.ResponseWriter, r *http.Request) bool {
|
||||
c, err := websocket.Accept(w, r, nil)
|
||||
|
||||
if err != nil {
|
||||
Log.Warnf("Websocket issue: %v", err)
|
||||
return
|
||||
log.Warnf("Websocket issue: %v", err)
|
||||
return false
|
||||
}
|
||||
|
||||
defer c.Close(websocket.StatusInternalError, "Goodbye")
|
||||
|
|
@ -23,14 +25,16 @@ func handleWebsocket(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
var v interface{}
|
||||
|
||||
err = wsjson.Read(Ctx, c, v)
|
||||
|
||||
if err != nil {
|
||||
Log.Warnf("Websocket issue: %v", err)
|
||||
return
|
||||
}
|
||||
err = wsjson.Read(ctx, c, v)
|
||||
|
||||
log.Printf("recv: %v", v)
|
||||
|
||||
if err != nil {
|
||||
log.Warnf("Websocket issue: %v", err)
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
c.Close(websocket.StatusNormalClosure, "")
|
||||
return true
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,11 +4,14 @@ import (
|
|||
"github.com/stretchr/testify/assert"
|
||||
"os"
|
||||
"testing"
|
||||
config "github.com/OliveTin/OliveTin/internal/config"
|
||||
)
|
||||
|
||||
func TestGetWebuiDir(t *testing.T) {
|
||||
os.Chdir("../../") // go test sets the cwd to "httpservers" by default
|
||||
|
||||
cfg = config.DefaultConfig();
|
||||
|
||||
dir := findWebuiDir()
|
||||
|
||||
assert.Equal(t, "./webui", dir, "Finding the webui dir")
|
||||
|
|
|
|||
|
|
@ -0,0 +1,32 @@
|
|||
export function setupWebsocket() {
|
||||
window.websocketAvailable = false
|
||||
|
||||
window.ws = new WebSocket('ws://localhost:1337')
|
||||
|
||||
ws.addEventListener('open', websocketOnOpen)
|
||||
ws.addEventListener('message', websocketOnMessage)
|
||||
ws.addEventListener('error', websocketOnError)
|
||||
ws.addEventListener('close', websocketOnClose)
|
||||
}
|
||||
|
||||
function websocketOnOpen(evt) {
|
||||
window.websocketAvailable = true
|
||||
console.log("open")
|
||||
|
||||
const foo = '{}'
|
||||
|
||||
ws.send(foo)
|
||||
}
|
||||
|
||||
function websocketOnMessage(msg) {
|
||||
console.log(msg)
|
||||
}
|
||||
|
||||
function websocketOnError(err) {
|
||||
window.websocketAvailable = false
|
||||
console.log(err)
|
||||
}
|
||||
|
||||
function websocketOnClose() {
|
||||
window.websocketAvailable = false
|
||||
}
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
'use strict'
|
||||
|
||||
import { marshalActionButtonsJsonToHtml, marshalLogsJsonToHtml } from './js/marshaller.js'
|
||||
import { setupWebsocket } from './js/websocket.js'
|
||||
|
||||
function showSection (name) {
|
||||
for (const otherName of ['Actions', 'Logs']) {
|
||||
|
|
@ -75,6 +76,8 @@ function processWebuiSettingsJson (settings) {
|
|||
function main () {
|
||||
setupSections()
|
||||
|
||||
setupWebsocket()
|
||||
|
||||
window.fetch('webUiSettings.json').then(res => {
|
||||
return res.json()
|
||||
}).then(res => {
|
||||
|
|
|
|||
Loading…
Reference in New Issue