Skip to content

Instantly share code, notes, and snippets.

@DaniyarGilimov
Created September 2, 2020 10:50
Show Gist options
  • Save DaniyarGilimov/9f7b2aa83aa9ab0d3987b790c78baee5 to your computer and use it in GitHub Desktop.
Save DaniyarGilimov/9f7b2aa83aa9ab0d3987b790c78baee5 to your computer and use it in GitHub Desktop.
// PlayerListener used to listen player via websocket
func PlayerListener(pc *model.PlayerConn) {
defer func(pc *model.PlayerConn) {
if r := recover(); r != nil {
log.Println("Recovered in PlayerListener", r)
}
if !pc.WR {
log.Printf("closing in listener %d", pc.PlayerID)
close(pc.Ch)
pc.Room.Leave <- pc
pc.WS.Close()
} else {
log.Printf("!closing in listener %d", pc.PlayerID)
}
}(pc)
pc.WR = false
pc.WS.SetReadLimit(utils.MaxMessageSize)
pc.WS.SetReadDeadline(time.Now().Add(utils.PongWait))
pc.WS.SetPongHandler(func(string) error { pc.WS.SetReadDeadline(time.Now().Add(utils.PongWait)); return nil })
pc.LastActivity = time.Now()
for {
_, command, err := pc.WS.ReadMessage()
if err != nil {
log.Print("socket error in listener" + err.Error())
break
}
si := model.StatusInstruction{}
json.Unmarshal(command, &si)
z := make([]byte, len(command))
copy(z, command)
log.Printf("%d PlayerListener: player sended this shit: %s", pc.Room.ID, string(z))
switch si.Status {
case utils.PlayerLeft:
log.Printf("%d PlayerListener: listened player left", pc.Room.ID)
goto Exit
case utils.MoveTurn, utils.LocalTurn, utils.LocalBetFold, utils.LocalBet, utils.MoveTurnBet:
pc.Room.RoomData.GameBridger.InstCh <- z
break
case utils.UtilEmoji:
if time.Now().Sub(pc.LastActivity) > (utils.LastActivityDuration * time.Second) {
go BroadcastState(z, pc.Room)
pc.LastActivity = time.Now()
}
break
case utils.UtilThrow:
if time.Now().Sub(pc.LastActivity) > (utils.LastActivityDuration * time.Second) {
go ThrowObject(z, pc)
pc.LastActivity = time.Now()
}
break
case utils.UtilInviteFriend:
log.Printf("%d PlayerListener: friend inviting", pc.Room.ID)
//pc.Room.RoomData.Lock()
go InviteFriend(z, pc)
//pc.Room.RoomData.Unlock()
break
case utils.UtilOpenCards:
go OpenCards(pc.Room.RoomData)
break
case utils.UtilCancleInvite:
go CancleInvite(z, pc.PlayerID)
log.Printf("%d PlayerListener: canceling invitation", pc.Room.ID)
break
default:
log.Printf("%d PlayerListener: Unexpected command is responsed it is: %s", pc.Room.ID, string(command))
goto Exit
}
log.Printf("%d PlayerListener: finished loop iteration", pc.Room.ID)
}
Exit:
}
// PlayerWriter pumps messages from the hub to the websocket connection.
func PlayerWriter(pc *model.PlayerConn) {
ticker := time.NewTicker(utils.PingPeriod)
defer func(pc *model.PlayerConn) {
ticker.Stop()
if !pc.WR {
log.Printf("closing in writer %d", pc.PlayerID)
pc.WS.Close()
log.Printf("closed in writer %d", pc.PlayerID)
} else {
log.Printf("!closed in writer %d", pc.PlayerID)
}
}(pc)
for {
select {
case message, ok := <-pc.Ch:
z := make([]byte, len(message))
copy(z, message)
pc.WS.SetWriteDeadline(time.Now().Add(utils.WriteWait))
if !ok {
log.Print("PlayerWriter: is not ok")
pc.WS.WriteMessage(websocket.CloseMessage, []byte{})
return
}
pc.Mu.Lock()
err := pc.WS.WriteMessage(websocket.TextMessage, z)
pc.Mu.Unlock()
if err != nil {
log.Print("PlayerWriter: error in write" + err.Error())
return
}
inst := &model.PlayerLeftInstruction{}
_ = json.Unmarshal(z, inst)
if inst.Instruction == utils.UtilAFK || inst.Instruction == utils.RoomMoneyLess {
pc.WS.WriteMessage(websocket.CloseMessage, []byte{})
log.Printf("PlayerWriter: Closing by AFK, RoomMoney less %d", pc.PlayerID)
return
}
break
case <-ticker.C:
pc.WS.SetWriteDeadline(time.Now().Add(utils.WriteWait))
if err := pc.WS.WriteMessage(websocket.PingMessage, nil); err != nil {
log.Printf("PlayerWriter: Closing by deadline %d", pc.PlayerID)
return
}
break
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment