websocket base and command parser
This commit is contained in:
@@ -1,17 +1,68 @@
|
||||
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
|
||||
func InitWSServer(dbConn *db.DBConn, twitchConn *ttv.TwitchConn) (*WSServer, error) {
|
||||
return &WSServer{}, nil
|
||||
}
|
||||
|
||||
type WSServer struct {
|
||||
}
|
||||
|
||||
func (wsServer *WSServer) Listen() {
|
||||
// start web server itself
|
||||
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) {
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user