Skip to content

Commit

Permalink
Added websocket client ping (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
andersevenrud committed Apr 11, 2020
1 parent 1d12093 commit 118e0ef
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ const defaultConfiguration = {
}
},
ws: {
port: null
port: null,
ping: 30 * 1000
},
proxy: [
/*
Expand Down
12 changes: 12 additions & 0 deletions src/providers/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,18 @@ class CoreServiceProvider extends ServiceProvider {
ws.upgradeReq = ws.upgradeReq || req;
ws._osjs_client = Object.assign({}, req.session.user);

const interval = this.core.config('ws.ping', 0);

const pingInterval = interval ? setInterval(() => {
ws.send(JSON.stringify({
name: 'osjs/core:ping'
}));
}, interval) : undefined;

ws.on('close', () => {
clearInterval(pingInterval);
});

ws.on('message', msg => {
try {
const {name, params} = JSON.parse(msg);
Expand Down

0 comments on commit 118e0ef

Please sign in to comment.