如何每隔60秒刷新一次会话并在Pharo的成绩单上显示?

时间:2017-11-24 12:46:42

标签: smalltalk pharo pharocloud pharo-5

|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.

我想实现一个方法,可以每隔60秒刷新一次ZnClient会话,以检查我登录的服务器上的新数据。我尝试了pharo的重定向方法,但它似乎不起作用。或者说它没有显示任何东西。有什么想法吗?

1 个答案:

答案 0 :(得分:2)

| 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。尽管有上面的示例代码,您可能需要考虑在ZnClient中尝试If-Modified-Since方法。