How can I refresh a session for every 60 second and display it on transcript with Pharo?

爱⌒轻易说出口 提交于 2019-12-02 06:34:29

问题


|a b |
a := ZnClient new.
a get: 'http://cloud-storage.com/login'.
a
formAt: 'username' put: 'jom';
formAt: 'password' put: 'mypass';
post;
get: 'http://cloud-storage.com/my-file'.
"Here I want to refresh the session for every 60sec and"
"to checking for newer data"
b := a maxNumberOfRedirects:60
Transcript show: b; cr.

I would like to implement a method that can refresh the ZnClient session every 60s for checking for newer data on the server I am logged into. I tried the redirect method of pharo but it does not seem to work. or say It does not show anything. Any idea?


回答1:


| session data |

session := ZnClient new url: 'http://cloud-storage.com'.

"Login"
session path: '/login';
    formAt: 'email' put: 'jom';
    formAt: 'password' put: 'mypass';
    post.

"Get data"
data := session path: '/my-file'; get; contents.

"Check for new data every 60 secs for maximum 100 tries"
[
    100 timesRepeat: [
        | newData |
        (Delay forSeconds: 60) wait.
        newData := session path: '/my-file'; get; contents.
        (data ~= newData) ifTrue: [Transcript show: newData; cr]
    ]
] fork.

NB. Despite above example code you may want to consider trying If-Modified-Since method in ZnClient.



来源:https://stackoverflow.com/questions/47473641/how-can-i-refresh-a-session-for-every-60-second-and-display-it-on-transcript-wit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!