Skip to content

Commit

Permalink
simple WS
Browse files Browse the repository at this point in the history
  • Loading branch information
dido18 committed Jan 13, 2025
1 parent 67db428 commit 1ff38df
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
21 changes: 21 additions & 0 deletions home.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<b>Your browser does not support WebSockets.</b>";
appendLog(item);
}
};

function getListMsgVisibility() {
// Check if the list visibility setting is saved in the localStorage.
let savedSetting = localStorage.getItem(LOCAL_STORAGE_KEY);
Expand Down Expand Up @@ -309,6 +329,7 @@
<body>
<div id="container">
<div class="logs">
<pre id="ws-log"></pre>
<pre id="log"></pre>
<pre id="log-list"></pre>
</div>
Expand Down
31 changes: 31 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ import (
_ "embed"
"encoding/json"
"flag"
"fmt"
"html/template"
"io"
"net/http"
"os"
"regexp"
"runtime"
Expand All @@ -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
)
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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) {

Check failure on line 575 in main.go

View workflow job for this annotation

GitHub Actions / check-style (./)

exported function ServeWS should have comment or be unexported
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!"))
}

0 comments on commit 1ff38df

Please sign in to comment.