import Nat "mo:base/Nat";
import Text "mo:base/Text";
actor Counter {
stable var counter = 0;
// Get the value of the counter.
public query func get() : async Nat {
return counter;
};
// Set the value of the counter.
public func set(n : Nat) : async () {
counter := n;
};
// Increment the value of the counter.
public func inc() : async () {
counter += 1;
};
public type Key = Text;
public type Path = Text;
public type ChunkId = Nat;
public type SetAssetContentArguments = {
key : Key;
sha256 : ?[Nat8];
chunk_ids : [ChunkId];
content_encoding : Text;
};
public type StreamingCallbackHttpResponse = {
token : ?StreamingCallbackToken;
body : [Nat8];
};
public type StreamingCallbackToken = {
key : Text;
sha256 : ?[Nat8];
index : Nat;
content_encoding : Text;
};
public type StreamingStrategy = {
#Callback : {
token : StreamingCallbackToken;
callback : shared query StreamingCallbackToken -> async StreamingCallbackHttpResponse;
};
};
public type HeaderField = (Text, Text);
public type HttpRequest = {
url : Text;
method : Text;
body : [Nat8];
headers : [HeaderField];
};
public type HttpResponse = {
body : Blob;
headers : [HeaderField];
streaming_strategy : ?StreamingStrategy;
status_code : Nat16;
};
public shared query func http_request(request: HttpRequest): async HttpResponse {
{
body = Text.encodeUtf8("<html><body><h1>" # Nat.toText(counter) # "</h1></body></html>");
headers = [];
streaming_strategy = null;
status_code = 200;
}
};
};
使用Motoko实现计数器
最新推荐文章于 2022-03-14 17:18:13 发布
本文介绍了一个名为actorCounter的组件,它提供了一个计数器功能,包括获取值、设置值和递增,以及HTTP请求响应,用于实时更新HTML页面。它展示了如何在信息技术中使用异步操作和流处理策略。
1901

被折叠的 条评论
为什么被折叠?



