Minimal ws connection

This commit is contained in:
Araozu 2024-05-21 09:09:23 -05:00
parent da97b0b7b6
commit ac638c3276
4 changed files with 62 additions and 5 deletions

42
controller/lobbyws.go Normal file
View File

@ -0,0 +1,42 @@
package controller
import (
"log"
"net/http"
"time"
"github.com/gorilla/websocket"
)
var upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
}
func LobbyWsConnect(writer http.ResponseWriter, request *http.Request) {
conn, err := upgrader.Upgrade(writer, request, nil)
if err != nil {
log.Print("upgrade:", err)
return
}
defer conn.Close()
for {
mt, message, err := conn.ReadMessage()
if err != nil {
log.Print("read:", err)
break
}
log.Printf("recv: %s, type: %d", message, mt)
time.Sleep(10 * time.Second)
err = conn.WriteMessage(mt, message)
if err != nil {
log.Print("write:", err)
break
}
}
}

5
go.mod
View File

@ -6,6 +6,11 @@ require (
github.com/gorilla/mux v1.8.1 github.com/gorilla/mux v1.8.1
github.com/nrednav/cuid2 v1.0.0 github.com/nrednav/cuid2 v1.0.0
github.com/rs/cors v1.11.0 github.com/rs/cors v1.11.0
)
require (
github.com/gorilla/websocket v1.5.1 // indirect
golang.org/x/crypto v0.23.0 // indirect golang.org/x/crypto v0.23.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/sys v0.20.0 // indirect golang.org/x/sys v0.20.0 // indirect
) )

4
go.sum
View File

@ -1,10 +1,14 @@
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
github.com/nrednav/cuid2 v1.0.0 h1:27dn1oGiG+23Wa8XJ2DHeMoMa18Zs9u1+UHI9IlcGKM= github.com/nrednav/cuid2 v1.0.0 h1:27dn1oGiG+23Wa8XJ2DHeMoMa18Zs9u1+UHI9IlcGKM=
github.com/nrednav/cuid2 v1.0.0/go.mod h1:pdRH5Zrjwnv8DZ74XvHR3jX+bzJNfQjwLQ3JgSI2EmI= github.com/nrednav/cuid2 v1.0.0/go.mod h1:pdRH5Zrjwnv8DZ74XvHR3jX+bzJNfQjwLQ3JgSI2EmI=
github.com/rs/cors v1.11.0 h1:0B9GE/r9Bc2UxRMMtymBkHTenPkHDv0CW4Y98GBY+po= github.com/rs/cors v1.11.0 h1:0B9GE/r9Bc2UxRMMtymBkHTenPkHDv0CW4Y98GBY+po=
github.com/rs/cors v1.11.0/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU= github.com/rs/cors v1.11.0/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI= golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=

16
main.go
View File

@ -20,11 +20,17 @@ type PersonInfo struct {
func main() { func main() {
fmt.Println("hello SEKAI!!") fmt.Println("hello SEKAI!!")
router := mux.NewRouter().PathPrefix("/api").Subrouter() mainRouter := mux.NewRouter()
httpRouter := mainRouter.PathPrefix("/api").Subrouter()
wsRouter := mainRouter.PathPrefix("/ws").Subrouter()
router.HandleFunc("/register", Register) // HTTP routes
router.HandleFunc("/validate", controller.ValidateId) httpRouter.HandleFunc("/register", Register)
router.HandleFunc("/lobby/new", controller.CreateLobby).Methods("POST") httpRouter.HandleFunc("/validate", controller.ValidateId)
httpRouter.HandleFunc("/lobby/new", controller.CreateLobby).Methods("POST")
// WS routes
wsRouter.HandleFunc("/lobby/connect", controller.LobbyWsConnect)
port, ok := os.LookupEnv("PORT") port, ok := os.LookupEnv("PORT")
if !ok { if !ok {
@ -38,7 +44,7 @@ func main() {
AllowCredentials: true, AllowCredentials: true,
}) })
handler := cors.Handler(router) handler := cors.Handler(mainRouter)
log.Fatal(http.ListenAndServe(":"+port, handler)) log.Fatal(http.ListenAndServe(":"+port, handler))
} }