Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a Gateway.connected property #325

Merged
merged 2 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions gateways/js/dist/esm/fjage.js
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,7 @@ class Gateway {
this.listener = {}; // set of callbacks that want to listen to incoming messages
this.eventListeners = {}; // external listeners wanting to listen internal events
this.queue = []; // incoming message queue
this.connected = false; // connection status
this.debug = false; // debug info to be logged to console?
this.aid = new AgentID((isBrowser ? 'WebGW-' : 'NodeGW-')+_guid(4)); // gateway agent name
this.connector = this._createConnector(url);
Expand Down Expand Up @@ -865,6 +866,7 @@ class Gateway {
} else return null;
conn.setReadCallback(this._onMsgRx.bind(this));
conn.addConnectionListener(state => {
this.connected = !!state;
if (state == true){
this.flush();
this.connector.write('{"alive": true}');
Expand Down
2 changes: 2 additions & 0 deletions gateways/js/dist/fjage.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions gateways/js/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions gateways/js/src/fjage.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ export class Gateway {
this.listener = {}; // set of callbacks that want to listen to incoming messages
this.eventListeners = {}; // external listeners wanting to listen internal events
this.queue = []; // incoming message queue
this.connected = false; // connection status
this.debug = false; // debug info to be logged to console?
this.aid = new AgentID((isBrowser ? 'WebGW-' : 'NodeGW-')+_guid(4)); // gateway agent name
this.connector = this._createConnector(url);
Expand Down Expand Up @@ -500,6 +501,7 @@ export class Gateway {
} else return null;
conn.setReadCallback(this._onMsgRx.bind(this));
conn.addConnectionListener(state => {
this.connected = !!state;
if (state == true){
this.flush();
this.connector.write('{"alive": true}');
Expand Down
14 changes: 14 additions & 0 deletions gateways/js/test/spec/fjage.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,20 @@ describe('A Gateway', function () {
expect(rxed.filter(r => !r).length).toBe(0);
});

it('should update the connected property when the underlying transport is disconnected/reconnected', async function() {
const gw = new Gateway(gwOpts);
gw.connector._reconnectTime = 300;
await delay(700);
expect([1, 'open']).toContain(gw.connector.sock.readyState);
if (isBrowser) gw.connector.sock.close();
else gw.connector.sock.destroy();
await delay(100);
expect(gw.connected).toBe(false);
await delay(1000);
expect(gw.connected).toBe(true);
gw.connector._reconnectTime = 5000;
});

it('should generate trigger connListener when the underlying transport is disconnected/reconnected', async function() {
const gw = new Gateway(gwOpts);
let spy = jasmine.createSpy('connListener');
Expand Down
Loading