Add a dummy echo websocket endpoint

This commit is contained in:
Zoe Roux 2024-03-15 23:01:57 +01:00
parent e47c6500f3
commit 61ee5e53fb
No known key found for this signature in database
3 changed files with 50 additions and 0 deletions

5
websockets/go.mod Normal file
View File

@ -0,0 +1,5 @@
module github.com/zoriya/kyoo/websockets
go 1.21.7
require nhooyr.io/websocket v1.8.10 // direct

2
websockets/go.sum Normal file
View File

@ -0,0 +1,2 @@
nhooyr.io/websocket v1.8.10 h1:mv4p+MnGrLDcPlBoWsvPP7XCzTYMXP9F9eIGoKbgx7Q=
nhooyr.io/websocket v1.8.10/go.mod h1:rN9OFWIUwuxg4fR5tELlYC04bXYowCP9GX47ivo2l+c=

43
websockets/main.go Normal file
View File

@ -0,0 +1,43 @@
package main
import (
"context"
"log"
"net/http"
"time"
websocket "nhooyr.io/websocket"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
c, err := websocket.Accept(w, r, &websocket.AcceptOptions{
InsecureSkipVerify: true,
})
if err != nil {
log.Println(err)
return
}
defer c.Close(websocket.StatusInternalError, "Internal error")
for {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
t, message, err := c.Read(ctx)
if err != nil || t != websocket.MessageText {
break
}
log.Printf("Received %v", message)
err = c.Write(ctx, websocket.MessageText, message)
if err != nil {
break
}
}
})
err := http.ListenAndServe(":7777", nil)
log.Fatal(err)
}