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