Skip to content

Commit 1ff38df

Browse files
committed
simple WS
1 parent 67db428 commit 1ff38df

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

Diff for: home.html

+21
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,26 @@
4343
});
4444
});
4545

46+
window.onload = function () {
47+
var conn;
48+
if (window["WebSocket"]) {
49+
console.log("Connecting to websocket", "ws://" + document.location.host + "/ws");
50+
conn = new WebSocket("ws://127.0.0.1:9001/ws");
51+
conn.onclose = function (evt) {
52+
console.log("Closing: " + evt);
53+
};
54+
conn.onmessage = function (evt) {
55+
console.log("Received: " + evt.data);
56+
const wsLog = document.getElementById("ws-log");
57+
wsLog.textContent = evt.data;
58+
};
59+
} else {
60+
var item = document.createElement("div");
61+
item.innerHTML = "<b>Your browser does not support WebSockets.</b>";
62+
appendLog(item);
63+
}
64+
};
65+
4666
function getListMsgVisibility() {
4767
// Check if the list visibility setting is saved in the localStorage.
4868
let savedSetting = localStorage.getItem(LOCAL_STORAGE_KEY);
@@ -309,6 +329,7 @@
309329
<body>
310330
<div id="container">
311331
<div class="logs">
332+
<pre id="ws-log"></pre>
312333
<pre id="log"></pre>
313334
<pre id="log-list"></pre>
314335
</div>

Diff for: main.go

+31
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ import (
2222
_ "embed"
2323
"encoding/json"
2424
"flag"
25+
"fmt"
2526
"html/template"
2627
"io"
28+
"net/http"
2729
"os"
2830
"regexp"
2931
"runtime"
@@ -45,6 +47,7 @@ import (
4547
cors "github.com/gin-contrib/cors"
4648
"github.com/gin-gonic/gin"
4749
"github.com/go-ini/ini"
50+
"github.com/gorilla/websocket"
4851
log "github.com/sirupsen/logrus"
4952
//"github.com/sanbornm/go-selfupdate/selfupdate" #included in update.go to change heavily
5053
)
@@ -463,6 +466,15 @@ func loop() {
463466
r.POST("/pause", pauseHandler)
464467
r.POST("/update", updateHandler)
465468

469+
// TODO: temporary using a different port for the websocket server
470+
go func() {
471+
http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
472+
ServeWS(w, r)
473+
})
474+
fmt.Println("Starting server and websocket on " + *address + ":9001")
475+
log.Fatal(http.ListenAndServe(*address+":9001", nil))
476+
}()
477+
466478
// Mount goa handlers
467479
goa := v2.Server(config.GetDataDir().String(), Index)
468480
r.Any("/v2/*path", gin.WrapH(goa))
@@ -557,3 +569,22 @@ func installCertsKeyExists(filename string) (bool, error) {
557569
func promptInstallCertsSafari() bool {
558570
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")
559571
}
572+
573+
var upgrader = websocket.Upgrader{}
574+
575+
func ServeWS(w http.ResponseWriter, r *http.Request) {
576+
upgrader.CheckOrigin = func(r *http.Request) bool {
577+
// TODO: check origin with the list of allowed origins
578+
return true
579+
}
580+
581+
ws, err := upgrader.Upgrade(w, r, nil)
582+
if err != nil {
583+
log.Println("upgrade:", err)
584+
return
585+
}
586+
587+
defer ws.Close()
588+
fmt.Println("[WS] Client connected")
589+
ws.WriteMessage(websocket.TextMessage, []byte("Hello, client!"))
590+
}

0 commit comments

Comments
 (0)