Files
LargeHadronCollider/api/ws/main.go
2026-01-06 23:43:14 -06:00

69 lines
1.2 KiB
Go

package ws
import (
"log"
"net/http"
"github.com/gobwas/ws"
"github.com/gobwas/ws/wsutil"
"zomo.dev/largehadroncollider/db"
"zomo.dev/largehadroncollider/ttv"
)
func InitWSServer(dbConn *db.DBConn, twitchConn *ttv.TwitchConn) (*WSServer, error) {
return &WSServer{}, nil
}
type WSServer struct {
}
type WSConn struct {
events []string
}
func (wsServer *WSServer) Handle(w http.ResponseWriter, r *http.Request) error {
conn, _, _, err := ws.UpgradeHTTP(r, w)
if err != nil {
return err
}
authrorization := ""
go func() {
defer conn.Close()
for {
msg, _, err := wsutil.ReadClientData(conn)
if err != nil {
// TODO handle error better, print more conn info
log.Printf("Error reading data from ws connection %v", err)
continue
}
cmd, err := parseCommand(string(msg))
if err != nil {
log.Printf("Error parsing command data from ws connection %v", err)
continue
}
log.Printf("%+v", cmd)
if authrorization == "" {
// TODO authorize connection
continue
}
// TODO run with cmd
// TODO errors will be responded to the client
// if the response errors, log and exit the loop
}
}()
return nil
}
func runCommand(cmd *Command) {
}