This repository has been archived by the owner on Jul 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
57 lines (49 loc) · 1.74 KB
/
main.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//Incomming message topic.
const toWebTopic = 'to-web-topic';
//OutGoing message topic.
const toServiceTopic = 'to-service-topic';
//The identity of the Node Service.
const serviceUuid = 'node-integration-example-service';
//the asset alias for the node service (found in the app.json)
const nodeServiceAlias = 'node-service';
//retreives the port for the current OpenFin process
function getPort() {
return new Promise((resolve, reject) => {
chrome.desktop.getDetails(d => resolve(d.port));
});
}
//launches the node-service
function launchNodeService(port) {
return new Promise((resolve, reject) => {
fin.desktop.System.launchExternalProcess({
alias: nodeServiceAlias,
arguments: 'index.js --port ' + port,
lifetime: 'window'
}, resolve
, (reason, error) => reject(error));
});
}
function sendIABMessage() {
fin.desktop.InterApplicationBus.send(serviceUuid, toServiceTopic, {
data: 'Hello Service',
timeStamp: Date.now()
});
}
function onFinReady() {
const messageCtrl = document.querySelector('#message');
const timeStampCtrl = document.querySelector('#time');
//subscribe to messages from the node service
fin.desktop.InterApplicationBus.subscribe(serviceUuid, toWebTopic, (msg, uuid) => {
messageCtrl.innerText = `Received ${msg.data} from ${uuid}`;
timeStampCtrl.innerText = new Date(msg.timeStamp).toLocaleTimeString();
});
//launch the node service.
getPort()
.then(launchNodeService)
.then(() => console.log('node service is running'))
.catch(err => console.log(err));
//send messages every second.
setInterval(sendIABMessage, 1000);
}
//Once OpenFin is ready.
fin.desktop.main(onFinReady);