f
This commit is contained in:
@@ -2,7 +2,6 @@ package controllers
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"log"
|
||||
"net/http"
|
||||
"server/models"
|
||||
@@ -23,10 +22,12 @@ func NewHub() *Hub {
|
||||
|
||||
func (h *Hub) readLoop(conn *websocket.Conn) {
|
||||
p := &models.Player{
|
||||
ID: uint32(len(h.Players) + 1),
|
||||
Conn: conn,
|
||||
InputX: 0,
|
||||
InputZ: 0,
|
||||
}
|
||||
|
||||
log.Println("Player connected:", p.ID)
|
||||
|
||||
h.Players[p.ID] = p
|
||||
@@ -49,28 +50,25 @@ func (h *Hub) readLoop(conn *websocket.Conn) {
|
||||
}
|
||||
|
||||
buf := bytes.NewReader(data)
|
||||
binary.Read(buf, binary.LittleEndian, &p.InputX)
|
||||
binary.Read(buf, binary.LittleEndian, &p.InputZ)
|
||||
log.Println(buf)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Hub) StartWS(w http.ResponseWriter, r *http.Request) {
|
||||
func (h *Hub) ws(w http.ResponseWriter, r *http.Request) {
|
||||
upgrader := websocket.Upgrader{
|
||||
CheckOrigin: func(r *http.Request) bool { return true },
|
||||
}
|
||||
|
||||
for {
|
||||
conn, err := upgrader.Upgrade(w, r, nil)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
continue
|
||||
}
|
||||
|
||||
go h.readLoop(conn)
|
||||
conn, err := upgrader.Upgrade(w, r, nil)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
go h.readLoop(conn)
|
||||
}
|
||||
|
||||
func (h *Hub) Start() {
|
||||
http.HandleFunc("/ws", h.StartWS)
|
||||
http.HandleFunc("/ws", h.ws)
|
||||
log.Fatal(http.ListenAndServe(":8080", nil))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user