Browse Source

Attempt to fix implementation of fullReload

master
Jason Chu 6 years ago
parent
commit
1d885db08c
  1. 6
      connect.go
  2. 2
      consts.go
  3. 21
      listen.go
  4. 4
      pull_form.go

6
connect.go

@ -169,14 +169,16 @@ func (s *Session) createStage1Request() (*http.Request, error) {
form := s.newPullForm()
req, _ := http.NewRequest(http.MethodGet, chatURL+form.encode(), nil)
req, _ := http.NewRequest(http.MethodGet,
chatURL+form.form().Encode(), nil)
req.Header = defaultHeader()
return req, nil
}
func (s *Session) connectToStage2() error {
req, _ := http.NewRequest(http.MethodGet, chatURL+s.l.form.encode(), nil)
req, _ := http.NewRequest(http.MethodGet,
chatURL+s.l.form.form().Encode(), nil)
req.Header = defaultHeader()
resp, err := s.doRequest(req)

2
consts.go

@ -18,7 +18,7 @@ const (
readStatusURL = "https://www.facebook.com/ajax/mercury/change_read_status.php"
sendMessageURL = "https://www.facebook.com/ajax/mercury/send_messages.php"
typingURL = "https://www.facebook.com/ajax/messaging/typ.php"
syncURL = "https://www.facebook.com/notifications/sync?"
syncURL = "https://www.facebook.com/notifications/sync/?"
profileURL = "https://www.facebook.com/chat/user_info/"
allProfileURL = "https://www.facebook.com/chat/user_info_all"
userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36"

21
listen.go

@ -171,7 +171,7 @@ func (s *Session) listenRequest() {
})
s.client.Jar.SetCookies(fbURL, cookies)
req, _ := http.NewRequest(http.MethodGet, chatURL+s.l.form.encode(),
req, _ := http.NewRequest(http.MethodGet, chatURL+s.l.form.form().Encode(),
nil)
req.Header = defaultHeader()
@ -279,12 +279,7 @@ func (s *Session) handleDeltaMessage(body string, meta pullMsgMeta) {
}
func (s *Session) fullReload() {
wg := new(sync.WaitGroup)
wg.Add(2)
go func() {
defer wg.Done()
func() {
form := make(url.Values)
form.Set("lastSync", strconv.FormatInt(s.l.lastSync.Unix(), 10))
form = s.addFormMeta(form)
@ -292,25 +287,23 @@ func (s *Session) fullReload() {
req, _ := http.NewRequest(http.MethodGet, syncURL+form.Encode(), nil)
req.Header = defaultHeader()
s.l.lastSync = time.Now()
resp, err := s.doRequest(req)
if err != nil {
s.l.onError(ListenError{"reload sync", err})
return
}
s.l.lastSync = time.Now()
resp.Body.Close()
}()
go func() {
defer wg.Done()
func() {
form := make(url.Values)
form.Set("client", "mercury")
form.Set("folders[0]", "inbox")
form.Set("last_action_timestamp",
strconv.FormatInt(time.Now().Unix()-60, 10))
strconv.FormatInt(time.Now().Unix(), 10))
form = s.addFormMeta(form)
req, _ := http.NewRequest(http.MethodPost, threadSyncURL,
@ -324,6 +317,4 @@ func (s *Session) fullReload() {
resp.Body.Close()
}()
wg.Wait()
}

4
pull_form.go

@ -34,7 +34,7 @@ func (s *Session) newPullForm() pullForm {
}
}
func (p pullForm) encode() string {
func (p pullForm) form() url.Values {
form := url.Values{
"channel": []string{"p_" + p.userID},
"seq": []string{strconv.Itoa(p.seq)},
@ -53,5 +53,5 @@ func (p pullForm) encode() string {
form.Set("sticky_pool", p.stickyPool)
}
return form.Encode()
return form
}

Loading…
Cancel
Save