This repository has been archived by the owner on Apr 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔨 allow for multiple calls to methods
- Loading branch information
Showing
4 changed files
with
36 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,24 @@ | ||
const WebSocket = require('ws'); | ||
const WebSocket = require('ws'), | ||
Run = require('./run'); | ||
|
||
module.exports = server => { | ||
const wss = new WebSocket.Server({ server }); | ||
|
||
wss.on('connection', ws => { | ||
let runningFuncs = []; | ||
console.log('New WS Connection'); | ||
ws.on('message', incoming = (message) => { | ||
ws.on('message', message => { | ||
message = JSON.parse(message); | ||
runningFuncs.map(rf => rf.name === message.request && rf.runner.stop()); | ||
switch (message.request) { | ||
case 'sql': require('./sql')(ws, message, {}); break; | ||
case 'sql': | ||
runningFuncs.push({ | ||
name: message.request, | ||
runner: new Run(ws, message, require('./sql')) | ||
}); | ||
break; | ||
} | ||
}).on('close', () => { | ||
runningFuncs.map(rf => rf.runner.stop()); | ||
}); | ||
// ws.on('close', () => { }); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module.exports = (ws, request, res) => { | ||
res.date = new Date(); | ||
ws.send(JSON.stringify(res)); | ||
console.log(`Response to '${request}' sent.`); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const respond = require('./respond'); | ||
let interval; | ||
|
||
class Run { | ||
constructor(ws, message, func) { | ||
func(message, res => respond(ws, message.request, res)); | ||
interval = setInterval(() => { | ||
func(message, res => respond(ws, message.request, res)); | ||
}, message.update_ms || 10000); | ||
} | ||
stop() { | ||
clearInterval(interval); | ||
} | ||
} | ||
|
||
module.exports = Run; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,7 @@ | ||
const sql = require('../common/sql'); | ||
|
||
const run = (ws, req) => { | ||
module.exports = (req, cb) => { | ||
sql(req, response => { | ||
response.date = new Date(); | ||
ws.send(JSON.stringify(response)); | ||
console.log(`Response to '${req.request}' sent.`); | ||
cb(response); | ||
}); | ||
}; | ||
|
||
module.exports = (ws, req) => { | ||
run(ws, req); | ||
setInterval(() => { | ||
run(ws, req); | ||
}, 10000); | ||
}; |