mirror of
https://github.com/giongto35/cloud-game.git
synced 2026-07-22 17:47:11 +00:00
Only maintains heartbeat from server to overlord
This commit is contained in:
parent
6381cfb889
commit
e19c10a219
3 changed files with 18 additions and 4 deletions
5
main.go
5
main.go
|
|
@ -73,12 +73,13 @@ func main() {
|
|||
}
|
||||
if len(os.Args) >= 3 {
|
||||
if os.Args[2] == "overlord" {
|
||||
fmt.Println("Running as overlord ")
|
||||
IsOverlord = true
|
||||
} else {
|
||||
fmt.Println("Running as slave ")
|
||||
// If the third arg is not overlord, it is path to overlord
|
||||
overlordHost = os.Args[2]
|
||||
}
|
||||
fmt.Println("Running as overlord ")
|
||||
}
|
||||
if len(os.Args) >= 4 {
|
||||
port = os.Args[3]
|
||||
|
|
@ -536,6 +537,8 @@ func NewOverlordClient() *Client {
|
|||
return req
|
||||
},
|
||||
)
|
||||
// heartbeat to keep pinging overlord. We not ping from server to browser, so we don't call heartbeat in browserClient
|
||||
go oclient.heartbeat()
|
||||
go oclient.listen()
|
||||
|
||||
return oclient
|
||||
|
|
|
|||
|
|
@ -62,6 +62,7 @@ func wso(w http.ResponseWriter, r *http.Request) {
|
|||
})
|
||||
|
||||
client.receive("getRoom", func(resp WSPacket) WSPacket {
|
||||
log.Println("Received a getroom request")
|
||||
return WSPacket{
|
||||
ID: "getRoom",
|
||||
Data: roomToServer[resp.Data],
|
||||
|
|
|
|||
16
ws.go
16
ws.go
|
|
@ -96,6 +96,16 @@ func (c *Client) syncSend(request WSPacket) (response WSPacket) {
|
|||
return <-res
|
||||
}
|
||||
|
||||
// heartbeat maintains connection to server
|
||||
func (c *Client) heartbeat() {
|
||||
// send heartbeat every 1s
|
||||
timer := time.Tick(time.Second)
|
||||
|
||||
for range timer {
|
||||
c.send(WSPacket{ID: "heartbeat"}, nil)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) listen() {
|
||||
for {
|
||||
log.Println("Waiting for message")
|
||||
|
|
@ -106,20 +116,20 @@ func (c *Client) listen() {
|
|||
}
|
||||
wspacket := WSPacket{}
|
||||
err = json.Unmarshal(rawMsg, &wspacket)
|
||||
if err != nil {
|
||||
if err != nil || wspacket.ID == "heartbeat" {
|
||||
continue
|
||||
}
|
||||
|
||||
// Check if some async send is waiting for the response based on packetID
|
||||
if callback, ok := c.sendCallback[wspacket.PacketID]; ok {
|
||||
callback(wspacket)
|
||||
go callback(wspacket)
|
||||
delete(c.sendCallback, wspacket.PacketID)
|
||||
// Skip receiveCallback to avoid duplication
|
||||
continue
|
||||
}
|
||||
// Check if some receiver with the ID is registered
|
||||
if callback, ok := c.recvCallback[wspacket.ID]; ok {
|
||||
callback(wspacket)
|
||||
go callback(wspacket)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue