Add heartbeat

This commit is contained in:
giongto35 2019-04-21 23:57:59 +08:00
parent bf3454d230
commit 281e044a7a
4 changed files with 13 additions and 9 deletions

1
Dockerfile vendored
View file

@ -13,6 +13,7 @@ RUN apt-get update
RUN apt-get install pkg-config libvpx-dev -y
RUN go get github.com/pion/webrtc
RUN go get github.com/gorilla/websocket
RUN go get github.com/satori/go.uuid
RUN go install github.com/giongto35/cloud-game
EXPOSE 8000

View file

@ -62,6 +62,8 @@ var serverID = ""
var oclient *Client
func main() {
rand.Seed(time.Now().UTC().UnixNano())
flag.Parse()
fmt.Println("Usage: ./game [debug]")
if *config.IsDebug {
@ -209,6 +211,10 @@ func ws(w http.ResponseWriter, r *http.Request) {
// The server session is maintaining
}
client.receive("heartbeat", func(resp WSPacket) WSPacket {
return resp
})
client.receive("initwebrtc", func(resp WSPacket) WSPacket {
log.Println("Received user SDP")
localSession, err := wssession.peerconnection.StartClient(resp.Data, width, height)
@ -401,12 +407,14 @@ func removeSession(w *webrtc.WebRTC, room *Room) {
}
func getServerIDOfRoom(oc *Client, roomID string) string {
log.Println("Request overlord roomID")
packet := oc.syncSend(
WSPacket{
ID: "getRoom",
Data: roomID,
},
)
log.Println("Received roomID from overlord")
return packet.Data
}

9
static/js/ws.js vendored
View file

@ -43,12 +43,7 @@ conn.onmessage = e => {
//pc.setRemoteDescription(new RTCSessionDescription(JSON.parse(atob(d["data"]))));
//conn.send(JSON.stringify({"id": "remotestart", "data": GAME_LIST[gameIdx].nes, "room_id": roomID.value, "player_index": parseInt(playerIndex.value, 10)}));inputTimer
//break;
case "pong":
// TODO: Change name use one session
log("Recv pong. Start webrtc");
//startWebRTC();
break;
case "pingpong":
case "heartbeat":
console.log("Ping: ", Date.now() - d["data"])
// TODO: Calc time
break;
@ -68,7 +63,7 @@ conn.onmessage = e => {
function sendPing() {
// TODO: format the package with time
conn.send(JSON.stringify({"id": "pingpong", "data": Date.now().toString()}));
conn.send(JSON.stringify({"id": "heartbeat", "data": Date.now().toString()}));
}
function startWebRTC() {

4
ws.go
View file

@ -108,7 +108,7 @@ func (c *Client) heartbeat() {
func (c *Client) listen() {
for {
log.Println("Waiting for message")
log.Println("Waiting for message ...")
_, rawMsg, err := c.conn.ReadMessage()
if err != nil {
log.Println("[!] read:", err)
@ -116,7 +116,7 @@ func (c *Client) listen() {
}
wspacket := WSPacket{}
err = json.Unmarshal(rawMsg, &wspacket)
if err != nil || wspacket.ID == "heartbeat" {
if err != nil {
continue
}