mirror of
https://github.com/fluencelabs/examples
synced 2024-12-04 19:20:17 +00:00
Add network communication for hello-world example
This commit is contained in:
parent
a9a97e7e84
commit
a9ab67b6fe
@ -1,5 +1,12 @@
|
||||
import Peer from "@fluencelabs/aqua-lib/builtin.aqua"
|
||||
|
||||
service HelloWorld("hello-world"):
|
||||
hello(str: string)
|
||||
|
||||
func sayHello():
|
||||
HelloWorld.hello("Hello, world!")
|
||||
|
||||
func getRelayTime() -> u64:
|
||||
on HOST_PEER_ID:
|
||||
ts <- Peer.timestamp_ms()
|
||||
<- ts
|
||||
|
13
js-sdk-examples/hello-world/package-lock.json
generated
13
js-sdk-examples/hello-world/package-lock.json
generated
@ -14,6 +14,7 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@fluencelabs/aqua": "^0.3.0-226",
|
||||
"@fluencelabs/aqua-lib": "^0.1.14",
|
||||
"chokidar-cli": "^3.0.0",
|
||||
"ts-node": "^10.2.1",
|
||||
"typescript": "^4.4.2"
|
||||
@ -82,6 +83,12 @@
|
||||
"aqua-j": "index-java.js"
|
||||
}
|
||||
},
|
||||
"node_modules/@fluencelabs/aqua-lib": {
|
||||
"version": "0.1.14",
|
||||
"resolved": "https://registry.npmjs.org/@fluencelabs/aqua-lib/-/aqua-lib-0.1.14.tgz",
|
||||
"integrity": "sha512-H2Q4gIvociUxc4J2mwmH0D+mrU2N2Z+enKCHgBCanMVEE2wZDsZ80GTbDKsQjEq+gpqbnJIk8lJBYW6lyvLJTg==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/@fluencelabs/avm": {
|
||||
"version": "0.14.4",
|
||||
"resolved": "https://registry.npmjs.org/@fluencelabs/avm/-/avm-0.14.4.tgz",
|
||||
@ -3241,6 +3248,12 @@
|
||||
"integrity": "sha512-9o0TdgsVNcBvifqo7VqIkN62P9EReE0LUxgWG1rGHi9yxJiGElvEBvrVUzqShffF66Ene7VGEe85lhFIGCfDgg==",
|
||||
"dev": true
|
||||
},
|
||||
"@fluencelabs/aqua-lib": {
|
||||
"version": "0.1.14",
|
||||
"resolved": "https://registry.npmjs.org/@fluencelabs/aqua-lib/-/aqua-lib-0.1.14.tgz",
|
||||
"integrity": "sha512-H2Q4gIvociUxc4J2mwmH0D+mrU2N2Z+enKCHgBCanMVEE2wZDsZ80GTbDKsQjEq+gpqbnJIk8lJBYW6lyvLJTg==",
|
||||
"dev": true
|
||||
},
|
||||
"@fluencelabs/avm": {
|
||||
"version": "0.14.4",
|
||||
"resolved": "https://registry.npmjs.org/@fluencelabs/avm/-/avm-0.14.4.tgz",
|
||||
|
@ -12,6 +12,7 @@
|
||||
"license": "ISC",
|
||||
"devDependencies": {
|
||||
"@fluencelabs/aqua": "^0.3.0-226",
|
||||
"@fluencelabs/aqua-lib": "^0.1.14",
|
||||
"chokidar-cli": "^3.0.0",
|
||||
"ts-node": "^10.2.1",
|
||||
"typescript": "^4.4.2"
|
||||
|
@ -142,3 +142,72 @@ export function registerHelloWorld(peer: FluencePeer, serviceId: string, service
|
||||
return Promise.race([promise, Promise.resolve()]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
export function getRelayTime(config?: {ttl?: number}) : Promise<number>;
|
||||
export function getRelayTime(peer: FluencePeer, config?: {ttl?: number}) : Promise<number>;
|
||||
export function getRelayTime(...args: any) {
|
||||
let peer: FluencePeer;
|
||||
|
||||
let config: any;
|
||||
if (FluencePeer.isInstance(args[0])) {
|
||||
peer = args[0];
|
||||
config = args[1];
|
||||
} else {
|
||||
peer = Fluence.getPeer();
|
||||
config = args[0];
|
||||
}
|
||||
|
||||
let request: RequestFlow;
|
||||
const promise = new Promise<number>((resolve, reject) => {
|
||||
const r = new RequestFlowBuilder()
|
||||
.disableInjections()
|
||||
.withRawScript(
|
||||
`
|
||||
(xor
|
||||
(seq
|
||||
(seq
|
||||
(call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-)
|
||||
(xor
|
||||
(call -relay- ("peer" "timestamp_ms") [] ts)
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1])
|
||||
)
|
||||
)
|
||||
(xor
|
||||
(call %init_peer_id% ("callbackSrv" "response") [ts])
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2])
|
||||
)
|
||||
)
|
||||
(call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 3])
|
||||
)
|
||||
|
||||
`,
|
||||
)
|
||||
.configHandler((h) => {
|
||||
h.on('getDataSrv', '-relay-', () => {
|
||||
return peer.getStatus().relayPeerId;
|
||||
});
|
||||
|
||||
h.onEvent('callbackSrv', 'response', (args) => {
|
||||
const [res] = args;
|
||||
resolve(res);
|
||||
});
|
||||
|
||||
h.onEvent('errorHandlingSrv', 'error', (args) => {
|
||||
const [err] = args;
|
||||
reject(err);
|
||||
});
|
||||
})
|
||||
.handleScriptError(reject)
|
||||
.handleTimeout(() => {
|
||||
reject('Request timed out for getRelayTime');
|
||||
})
|
||||
if(config && config.ttl) {
|
||||
r.withTTL(config.ttl)
|
||||
}
|
||||
request = r.build();
|
||||
});
|
||||
peer.internals.initiateFlow(request!);
|
||||
return promise;
|
||||
}
|
||||
|
||||
|
@ -1,8 +1,13 @@
|
||||
import { Fluence } from "@fluencelabs/fluence";
|
||||
import { registerHelloWorld, sayHello } from "./_aqua/hello-world";
|
||||
import { krasnodar } from "@fluencelabs/fluence-network-environment";
|
||||
import {
|
||||
registerHelloWorld,
|
||||
sayHello,
|
||||
getRelayTime,
|
||||
} from "./_aqua/hello-world";
|
||||
|
||||
async function main() {
|
||||
await Fluence.start();
|
||||
await Fluence.start({ connectTo: krasnodar[0] });
|
||||
|
||||
registerHelloWorld({
|
||||
hello: async (str) => {
|
||||
@ -12,6 +17,10 @@ async function main() {
|
||||
|
||||
await sayHello();
|
||||
|
||||
const relayTime = await getRelayTime();
|
||||
|
||||
console.log("The relay time is: ", new Date(relayTime).toLocaleString());
|
||||
|
||||
await Fluence.stop();
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user