webAppServer/app/webAppClient.html
Sai Raj 149ada4c9b
script name change
Changing script name to webappClient
2020-02-26 15:47:54 +05:30

110 lines
4.4 KiB
HTML

<html>
<head>
<script id="floGlobals">
/* Constants for FLO blockchain operations !!Make sure to add this at begining!! */
const floGlobals = {
blockchain: "FLO",
adminID: "FKAEdnPfjXLHSYwrXQu377ugN4tXU7VGdf",
webAppURL: "<website_url/ip_addr>:<port>",
application: "TEST_MODE"
}
</script>
</head>
<body>
TEST_MODE (use console)
<script id="webappClient">
//Add this to the client script to request data from floWebappServer
const webappClient = {
util: {
sendData(data) {
return new Promise((resolve, reject) => {
var websocket = new WebSocket("wss://" + floGlobals.webAppURL + "/ws");
websocket.onmessage = (evt => {
if (evt.data == '$+') {
websocket.send(data);
resolve(`Data sent to webApp`);
} else if (evt.data == '$-')
reject(`webApp not available`)
else
reject(evt.data)
websocket.close();
})
websocket.onerror = (evt) => {
reject(`webApp server not found`)
};
})
},
requestData(request = []) {
return new Promise((resolve, reject) => {
var websocket = new WebSocket("wss://" + floGlobals.webAppURL + "/ws");
websocket.onmessage = (evt => {
if (evt.data == '$+') {
websocket.send(`?${JSON.stringify(request)}`);
} else if (evt.data == '$-') {
reject(`webApp not available`)
websocket.close()
} else {
resolve(JSON.parse(evt.data));
websocket.close();
}
})
websocket.onerror = (evt) => {
reject(`webApp server not found`)
};
})
}
},
sendGeneralData: function (message, type, options = {}) {
return new Promise((resolve, reject) => {
var data = {
senderID: myFloID,
receiverID: options.receiverID || floGlobals.adminID,
pubKey: myPubKey,
message: message,
sign: floCrypto.signData(JSON.stringify(message), myPrivKey),
application: options.application || floGlobals.application,
type: type,
comment: options.comment || ""
}
this.util.sendData(JSON.stringify(data), data.receiverID)
.then(result => resolve(result))
.catch(error => reject(error))
})
},
requestGeneralData: function (type, vectorClock = '0') {
if (typeof vectorClock !== 'string')
vectorClock.toString()
return new Promise((resolve, reject) => {
this.util.requestData([floGlobals.application, 'generalData', type, vectorClock])
.then(
result => {
if (result[0])
resolve(result[1])
else
reject(result[1])
}).catch(error => reject(error))
})
},
requestObjectData: function (keyPath) {
return new Promise((resolve, reject) => {
this.util.requestData([floGlobals.application, 'appObjects'].concat(keyPath)).then(
result => {
if (result[0])
resolve(result[1])
else
reject(result[1])
}).catch(error => reject(error))
})
}
}
</script>
</body>
</html>