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