From 1ff38dfad8efede545a676427288f8cc6d67583f Mon Sep 17 00:00:00 2001 From: Davide N Date: Mon, 13 Jan 2025 16:47:20 +0100 Subject: [PATCH] simple WS --- home.html | 21 +++++++++++++++++++++ main.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/home.html b/home.html index ee523981..84130b2e 100644 --- a/home.html +++ b/home.html @@ -43,6 +43,26 @@ }); }); + window.onload = function () { + var conn; + if (window["WebSocket"]) { + console.log("Connecting to websocket", "ws://" + document.location.host + "/ws"); + conn = new WebSocket("ws://127.0.0.1:9001/ws"); + conn.onclose = function (evt) { + console.log("Closing: " + evt); + }; + conn.onmessage = function (evt) { + console.log("Received: " + evt.data); + const wsLog = document.getElementById("ws-log"); + wsLog.textContent = evt.data; + }; + } else { + var item = document.createElement("div"); + item.innerHTML = "Your browser does not support WebSockets."; + appendLog(item); + } + }; + function getListMsgVisibility() { // Check if the list visibility setting is saved in the localStorage. let savedSetting = localStorage.getItem(LOCAL_STORAGE_KEY); @@ -309,6 +329,7 @@
+

         

         

       
diff --git a/main.go b/main.go index 1ca857b0..5d0e48b8 100755 --- a/main.go +++ b/main.go @@ -22,8 +22,10 @@ import ( _ "embed" "encoding/json" "flag" + "fmt" "html/template" "io" + "net/http" "os" "regexp" "runtime" @@ -45,6 +47,7 @@ import ( cors "github.com/gin-contrib/cors" "github.com/gin-gonic/gin" "github.com/go-ini/ini" + "github.com/gorilla/websocket" log "github.com/sirupsen/logrus" //"github.com/sanbornm/go-selfupdate/selfupdate" #included in update.go to change heavily ) @@ -463,6 +466,15 @@ func loop() { r.POST("/pause", pauseHandler) r.POST("/update", updateHandler) + // TODO: temporary using a different port for the websocket server + go func() { + http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) { + ServeWS(w, r) + }) + fmt.Println("Starting server and websocket on " + *address + ":9001") + log.Fatal(http.ListenAndServe(*address+":9001", nil)) + }() + // Mount goa handlers goa := v2.Server(config.GetDataDir().String(), Index) r.Any("/v2/*path", gin.WrapH(goa)) @@ -557,3 +569,22 @@ func installCertsKeyExists(filename string) (bool, error) { func promptInstallCertsSafari() bool { return utilities.UserPrompt("The Arduino Agent needs a local HTTPS certificate to work correctly with Safari.\nIf you use Safari, you need to install it.", "{\"Do not install\", \"Install the certificate for Safari\"}", "Install the certificate for Safari", "Install the certificate for Safari", "Arduino Agent: Install certificate") } + +var upgrader = websocket.Upgrader{} + +func ServeWS(w http.ResponseWriter, r *http.Request) { + upgrader.CheckOrigin = func(r *http.Request) bool { + // TODO: check origin with the list of allowed origins + return true + } + + ws, err := upgrader.Upgrade(w, r, nil) + if err != nil { + log.Println("upgrade:", err) + return + } + + defer ws.Close() + fmt.Println("[WS] Client connected") + ws.WriteMessage(websocket.TextMessage, []byte("Hello, client!")) +}