Finished support for sending presense/game status updates. Closes #18
This commit is contained in:
parent
bd1e20549a
commit
383b4645da
1 changed files with 31 additions and 12 deletions
43
wsapi.go
43
wsapi.go
|
@ -56,24 +56,43 @@ func (s *Session) Handshake() (err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateStatus is used to update the authenticated user's status.
|
type updateStatusData struct {
|
||||||
func (s *Session) UpdateStatus(idleSince, gameID string) (err error) {
|
IdleSince json.Token `json:"idle_since"`
|
||||||
|
GameID json.Token `json:"game_id"`
|
||||||
|
}
|
||||||
|
|
||||||
err = s.wsConn.WriteJSON(map[string]interface{}{
|
type updateStatusOp struct {
|
||||||
"op": 2,
|
Op int `json:"op"`
|
||||||
"d": map[string]interface{}{
|
Data updateStatusData `json:"d"`
|
||||||
"idle_since": idleSince,
|
}
|
||||||
"game_id": gameID,
|
|
||||||
},
|
// UpdateStatus is used to update the authenticated user's status.
|
||||||
})
|
// If idle>0 then set status to idle. If game>0 then set game.
|
||||||
|
// if otherwise, set status to active, and no game.
|
||||||
|
func (s *Session) UpdateStatus(idle int, gameID int) (err error) {
|
||||||
|
|
||||||
|
var usd updateStatusData
|
||||||
|
if idle > 0 {
|
||||||
|
usd.IdleSince = idle
|
||||||
|
} else {
|
||||||
|
usd.IdleSince = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if gameID >= 0 {
|
||||||
|
usd.GameID = gameID
|
||||||
|
} else {
|
||||||
|
usd.GameID = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
data := updateStatusOp{3, usd}
|
||||||
|
err = s.wsConn.WriteJSON(data)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: need a channel or something to communicate
|
|
||||||
// to this so I can tell it to stop listening
|
|
||||||
|
|
||||||
// Listen starts listening to the websocket connection for events.
|
// Listen starts listening to the websocket connection for events.
|
||||||
func (s *Session) Listen() (err error) {
|
func (s *Session) Listen() (err error) {
|
||||||
|
// TODO: need a channel or something to communicate
|
||||||
|
// to this so I can tell it to stop listening
|
||||||
|
|
||||||
if s.wsConn == nil {
|
if s.wsConn == nil {
|
||||||
fmt.Println("No websocket connection exists.")
|
fmt.Println("No websocket connection exists.")
|
||||||
|
|
Loading…
Reference in a new issue