Skip to content

Commit

Permalink
added path condition to socket server
Browse files Browse the repository at this point in the history
  • Loading branch information
hardikroongta8 committed Mar 29, 2024
1 parent d42f413 commit 27e4e2d
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 116 deletions.
12 changes: 11 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,17 @@ const securityKeyMiddleware = require("./middlewares/securityKeyMiddleware");

const app = express();
const server = http.createServer(app);
const wss = new WebSocket.Server({ server , path: '/ws'});
const wss = new WebSocket.Server({ noServer: true });

server.on('upgrade', (req, socket, head) => {
if(req.url === '/ws'){
wss.handleUpgrade(req, socket, head, (ws) => {
wss.emit('connection', ws, req);
});
} else {
socket.destroy();
}
});

app.use(express.json());
app.use(securityKeyMiddleware);
Expand Down
2 changes: 0 additions & 2 deletions helpers/websocketsHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@ exports.authenticateConnection = async(socket, req) => {
}
}catch(err){
if(axios.isAxiosError(err)){
console.log(err.response);
socket.send(JSON.stringify({
success: false,
statusCode: err.response.status,
error: err.response.data.error,
message: err.response.data.message
}));
}else{
console.log(err);
socket.send(JSON.stringify({
success: false,
statusCode: err.statusCode,
Expand Down
116 changes: 4 additions & 112 deletions package-lock.json

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

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"dependencies": {
"axios": "^1.6.8",
"express": "^4.19.2",
"http": "^0.0.1-security",
"mongoose": "^8.2.3",
"uuid": "^9.0.1",
"ws": "^8.16.0"
Expand Down

0 comments on commit 27e4e2d

Please sign in to comment.