mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-05-24 02:02:36 -04:00
Add a dummy echo websocket endpoint
This commit is contained in:
parent
e47c6500f3
commit
61ee5e53fb
5
websockets/go.mod
Normal file
5
websockets/go.mod
Normal 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
2
websockets/go.sum
Normal 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
43
websockets/main.go
Normal 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)
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user