citrineos-core
citrineos-core copied to clipboard
Add websocket "ping" handler
Adding a websocket "ping" handler to WebsocketNetworkConnection
to send pongs back with the same incoming payload.
This merges into rc-1.2.0
because I'm not certain what release this will be a part of; please let me know if I need to retarget!
I tested this locally with a quick node.js script to open a websocket connection and am properly receiving pongs from a locally running Citrine instance.
Local code sample
const { WebSocket } = require('ws');
main = () => {
console.log('opening websocket to citrine');
const ws = new WebSocket('ws://localhost:8081/Charger2', 'ocpp2.0.1');
ws.onopen = () => {
console.log('sending boot notification');
var BN = JSON.stringify([2, '1231231231231231', 'BootNotification', {
'chargingStation': {
'vendorName': 'local',
'model': 'localCharger',
'serialNumber': 'CHARGER02'
},
'reason': 'PowerUp'
}]);
ws.send(BN);
console.log('pinging');
ws.ping('hello!');
}
ws.on('pong', (data) => {
console.log(`received pong with data ${JSON.stringify(data)} -- closing websocket now`);
ws.close();
});
}
main();
Resulting local logs from ping/pong
- opening websocket to citrine
- sending boot notification
- pinging
- received pong with data {"type":"Buffer","data":[104,101,108,108,111,33]} -- closing websocket now