-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Se modifica el directorio del notifier.
- Loading branch information
1 parent
b020238
commit 4275208
Showing
7 changed files
with
1,519 additions
and
6 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
Submodule delta_notifier
deleted from
a606af
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,4 @@ | ||
SECRET=secret | ||
PRIVATE_KEY=123 | ||
WS_PORT=8080 | ||
PORT=3001 |
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,70 @@ | ||
require('dotenv').config() | ||
const express = require('express') | ||
const jwt = require('jsonwebtoken'); | ||
const WebSocket = require("ws"); | ||
|
||
|
||
const app = express() | ||
const port = 3001 | ||
|
||
const webSocketPort = 8080; | ||
const wss = new WebSocket.Server({port: webSocketPort}); | ||
console.log(`[WebSocket] Starting WebSocket server on localhost:${webSocketPort}`); | ||
|
||
app.use(express.json()); | ||
|
||
app.post('/onUpdate', authenticate, (req, res) => { | ||
res.send(`deviceId: ${req.body.deviceId} latitude: ${req.body.latitude} longitude: ${req.body.longitude}`); | ||
|
||
console.log(`${wss.clients.size}`); | ||
wss.clients.forEach((client) => { | ||
if (client.readyState === WebSocket.OPEN) { | ||
client.send(JSON.stringify(req.body)); | ||
} | ||
}); | ||
}) | ||
|
||
app.post('/auth', (req, res) => { | ||
const {body} = req | ||
const {privateKey} = body | ||
|
||
if (privateKey && privateKey === process.env.PRIVATE_KEY) { | ||
const token = jwt.sign( | ||
{id: 'DeltaApp'}, | ||
process.env.SECRET, | ||
{ | ||
expiresIn: 60 * 60 * 24 | ||
} | ||
) | ||
res.send({token}) | ||
} else { | ||
res.status(401).json({ | ||
error: 'Invalid credentials' | ||
}) | ||
} | ||
}) | ||
|
||
function authenticate(req, res, next) { | ||
const authorization = req.get('authorization') | ||
let token = '' | ||
|
||
if (authorization && authorization.toLowerCase().startsWith('bearer')) { | ||
token = authorization.substring(7) | ||
} | ||
try { | ||
jwt.verify(token, process.env.SECRET) | ||
} catch (e) { | ||
return res.status(401).json({error: 'token missing or invalid'}) | ||
} | ||
|
||
next() | ||
} | ||
|
||
|
||
app.listen(port, () => { | ||
console.log(`Example app listening on port ${port}`) | ||
}) | ||
|
||
wss.on("connection", (ws, request) => { | ||
console.log('[WebSocket] has connected'); | ||
}); |
Oops, something went wrong.