-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathping-pong.js
41 lines (33 loc) · 876 Bytes
/
ping-pong.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
let { Actor, ActorSystem } = require('../dist/index')
let sleep = async (time) => new Promise((resolve) => setTimeout(resolve, time))
let currentTime = +(new Date())
let timestamp = () => ((+new Date()) - currentTime)
class Ping extends Actor {
constructor() {
super("Ping")
this.times = 0
}
async ping(to) {
console.log(++this.times, timestamp(), "Ping")
to.pong(this.self)
return await sleep(100)
}
}
class Pong extends Actor {
constructor() {
super("Pong")
this.times = 0
}
async pong(to) {
console.log(++this.times, timestamp(), "Pong")
to.ping(this.self)
return await sleep(100)
}
}
let system = ActorSystem.default()
let ping = system.actorOf(Ping, [])
let pong = system.actorOf(Pong, [])
ping.ping(pong)
setTimeout(() => {
system.free()
}, 1000)