Request
#func Request(method: str, url: str, body: str, content_type: str, timeout_ms: int, done: func(int, str))Sends an HTTP request: method, address, body, content type, time limit in milliseconds (0 for none) and the function that receives the status and the response text. The status is 0 when there is no answer.
import dom
import web.browser
mut answer := "waiting"
mut polls := 0
func answered(status: int, text: str):
if status == 200:
answer = text
else:
dom.SetTimeout((): ask() end, 2000)
end
end
func ask():
polls++
browser.Get("https://example.com/chat/latest", 5000, (status: int, text: str): answered(status, text) end)
end
func deleted(status: int, text: str):
answer = status + " " + text
end
func remove():
browser.Request("DELETE", "https://example.com/chat/latest", "", "", 5000, (status: int, text: str): deleted(status, text) end)
end
func copy_link():
browser.CopyText("https://example.com/chat")
end
page:
title = "Chat"
box:
button:
content = "Start"
click = (): ask() end
end
button:
content = "Copy the link"
click = (): copy_link() end
end
button:
content = "Delete"
click = (): remove() end
end
paragraph:
content = polls + ": " + answer
end
end
end