diff --git a/api.go b/api.go index 59483b6..d905233 100644 --- a/api.go +++ b/api.go @@ -29,7 +29,7 @@ func ServeWs(hub *Hub, w http.ResponseWriter, r *http.Request) { client.PreviousMessages() // This will broadcast that this client is online to all users who have this client as a contact - client.ShareStatus() + client.ShareStatus("online") // Allow collection of memory referenced by the caller by doing all work in // new goroutines. diff --git a/client.go b/client.go index 59f413f..597d2a0 100644 --- a/client.go +++ b/client.go @@ -163,14 +163,14 @@ func (c *Client) PreviousMessages() { // ShareStatus will send `status` messages to all connected clients that have registered // the current client as a contact. -func (c *Client) ShareStatus() { +func (c *Client) ShareStatus(status string) { contacts, err := getContacts(c.ID, c.db) if err == nil { log.Printf("Contacts: %v", contacts) for _, contact := range contacts { if client, ok := c.hub.clients[contact]; ok { log.Printf("Client is online: %v", client.ID) - client.conn.WriteJSON(Response{Status: c.ID}) + client.conn.WriteJSON(Response{Status: StatusResponse{Mobile: c.ID, ConnectionStatus: status}}) } } } diff --git a/hub.go b/hub.go index 4049388..e017554 100644 --- a/hub.go +++ b/hub.go @@ -43,7 +43,7 @@ func (h *Hub) Run() { case client := <-h.unregister: log.Printf("client ID: %s got disconnected", client.ID) if _, ok := h.clients[client.ID]; ok { - client.ShareStatus() + client.ShareStatus("offline") delete(h.clients, client.ID) close(client.send) } diff --git a/types.go b/types.go index 6bcdd79..38924b8 100644 --- a/types.go +++ b/types.go @@ -4,10 +4,17 @@ import ( "encoding/json" ) +// StatusResponse is the response send as part of Response in case we are sending +// a status (online/offline) message. +type StatusResponse struct { + Mobile string `json:"mobile,omitempty"` + ConnectionStatus string `json:"connection_status,omitempty"` +} + // Response is the object returned by the api in all cases. type Response struct { // Status will be null if we're sending chat messages - Status string `json:"status,omitempty"` + Status StatusResponse `json:"status,omitempty"` // Messages will be null if we're sending status of a user Messages []Message `json:"messages,omitempty"`