From d59d47967c0b41fdb3d88a5453fea00c3707660c Mon Sep 17 00:00:00 2001 From: Bruce Marriner Date: Fri, 18 Dec 2015 15:09:34 -0600 Subject: [PATCH] Converted to using structs for data websocket handshake. Also using VERSION variable in $browser setting --- wsapi.go | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/wsapi.go b/wsapi.go index fe634de..3c5fe89 100644 --- a/wsapi.go +++ b/wsapi.go @@ -31,28 +31,31 @@ func (s *Session) Open() (err error) { return } -// maybe this is SendOrigin? not sure the right name here -// also bson.M vs string interface map? Read about -// how to send JSON the right way. +type handshakeProperties struct { + OS string `json:"$os"` + Browser string `json:"$browser"` + Device string `json:"$device"` + Referer string `json:"$referer"` + ReferringDomain string `json:"$referring_domain"` +} + +type handshakeData struct { + Version int `json:"v"` + Token string `json:"token"` + Properties handshakeProperties `json:"properties"` +} + +type handshakeOp struct { + Op int `json:"op"` + Data handshakeData `json:"d"` +} // Handshake sends the client data to Discord during websocket initial connection. func (s *Session) Handshake() (err error) { + // maybe this is SendOrigin? not sure the right name here - err = s.wsConn.WriteJSON(map[string]interface{}{ - "op": 2, - "d": map[string]interface{}{ - "v": 3, - "token": s.Token, - "properties": map[string]string{ - "$os": runtime.GOOS, - "$browser": "Discordgo", - "$device": "Discordgo", - "$referer": "", - "$referring_domain": "", - }, - }, - }) - + data := handshakeOp{2, handshakeData{3, s.Token, handshakeProperties{runtime.GOOS, "DiscordGo v" + VERSION, "", "", ""}}} + err = s.wsConn.WriteJSON(data) return }