summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxengineering <me@xengineering.eu>2026-03-21 16:38:56 +0100
committerxengineering <me@xengineering.eu>2026-03-22 10:23:46 +0100
commit139572f5221abd3f6ce1f0627982974ab3fa5531 (patch)
tree095123b92104c9265a53829536e462dcc7313f2c
parent89352c3950f7b72812d1d96be5931dc8420767f4 (diff)
downloadsia-server-139572f5221abd3f6ce1f0627982974ab3fa5531.tar
sia-server-139572f5221abd3f6ce1f0627982974ab3fa5531.tar.zst
sia-server-139572f5221abd3f6ce1f0627982974ab3fa5531.zip
tools: websocket: Add connection set up / tear down
This was validated by sniffing the communication with Wireshark. The following is executed: - TCP initial handshake - GET /rpc from tool - HTTP 101 Switching Protocols (to Websocket) from Shelly - ACK by tool - TCP connection close initiated by tool This shows that the tool is able to make Websocket connections.
-rw-r--r--tools/websocket.go42
1 files changed, 40 insertions, 2 deletions
diff --git a/tools/websocket.go b/tools/websocket.go
index e429a6d..575bcd5 100644
--- a/tools/websocket.go
+++ b/tools/websocket.go
@@ -1,9 +1,47 @@
+// Websocket debug tool
+//
+// Usage: ./websocket-linux-amd64 ws://<shelly-ip>/rpc
+//
+// This tools is intended to support development of the Websocket-based
+// application programming interface (API) of the Shelly Internet of Things
+// (IoT) devices.
+
package main
import (
- "fmt"
+ "log"
+ "net/url"
+ "os"
+ "os/signal"
+
+ "github.com/gorilla/websocket"
)
func main() {
- fmt.Println("Websockets are cool.")
+ log.SetFlags(0)
+
+ interrupt := make(chan os.Signal, 1)
+ signal.Notify(interrupt, os.Interrupt)
+
+ var u url.URL = getURL()
+ log.Printf("connecting to %s", u.String())
+
+ c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
+ if (err != nil) {
+ log.Fatal(err)
+ }
+ defer c.Close()
+}
+
+func getURL() url.URL {
+ if (len(os.Args) != 2) {
+ log.Fatalf("Exactly one argument expected but got %d.", len(os.Args) - 1)
+ }
+
+ maybeURL, err := url.Parse(os.Args[1])
+ if (err != nil) {
+ log.Fatalf("Cannot parse given URL: %s", os.Args[1])
+ }
+
+ return *maybeURL
}