commit 4a7e8b79c4104c6c76cdfee67e97d7833e325abf Author: GitProtogen Date: Mon Mar 9 14:45:36 2026 +0100 ws works diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..ab1f416 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,10 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Ignored default folder with query files +/queries/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/go-socket.iml b/.idea/go-socket.iml new file mode 100644 index 0000000..5e764c4 --- /dev/null +++ b/.idea/go-socket.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/go.imports.xml b/.idea/go.imports.xml new file mode 100644 index 0000000..d7202f0 --- /dev/null +++ b/.idea/go.imports.xml @@ -0,0 +1,11 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..45c5968 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/go-socket b/go-socket new file mode 100755 index 0000000..853a0a3 Binary files /dev/null and b/go-socket differ diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..83804e5 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module go-socket + +go 1.26 + +require nhooyr.io/websocket v1.8.17 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..9c3072b --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +nhooyr.io/websocket v1.8.17 h1:KEVeLJkUywCKVsnLIDlD/5gtayKp8VoCkksHCGGfT9Y= +nhooyr.io/websocket v1.8.17/go.mod h1:rN9OFWIUwuxg4fR5tELlYC04bXYowCP9GX47ivo2l+c= diff --git a/main.go b/main.go new file mode 100644 index 0000000..ea78ed5 --- /dev/null +++ b/main.go @@ -0,0 +1,75 @@ +package main + +import ( + "context" + "log" + "net/http" + "time" + + "nhooyr.io/websocket" + "nhooyr.io/websocket/wsjson" +) + +type Server struct { + OnOpen func(conn *websocket.Conn) + OnClose func(conn *websocket.Conn, err error) + OnMessage func(conn *websocket.Conn, msg map[string]any) +} + +func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { + conn, err := websocket.Accept(w, r, &websocket.AcceptOptions{ + InsecureSkipVerify: true, + }) + if err != nil { + log.Println("accept error:", err) + return + } + defer conn.CloseNow() + + if s.OnOpen != nil { + s.OnOpen(conn) + } + + ctx, cancel := context.WithTimeout(r.Context(), 30*time.Second) + defer cancel() + + var readErr error + for { + var msg map[string]any + if readErr = wsjson.Read(ctx, conn, &msg); readErr != nil { + break + } + if s.OnMessage != nil { + s.OnMessage(conn, msg) + } + } + + if s.OnClose != nil { + s.OnClose(conn, readErr) + } + + conn.Close(websocket.StatusNormalClosure, "done") +} + +func main() { + srv := &Server{ + OnOpen: func(conn *websocket.Conn) { + log.Println("client connected") + }, + OnClose: func(conn *websocket.Conn, err error) { + log.Println("client disconnected:", err) + }, + OnMessage: func(conn *websocket.Conn, msg map[string]any) { + log.Printf("received: %v\n", msg) + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + if err := wsjson.Write(ctx, conn, map[string]any{"echo": msg}); err != nil { + log.Println("write error:", err) + } + }, + } + + http.Handle("/ws", srv) + log.Println("server listening on :8080") + log.Fatal(http.ListenAndServe(":8080", nil)) +}