cleanup and adding in socket connection

This commit is contained in:
talksik
2022-06-03 14:09:30 -07:00
parent 58e5b68401
commit 9cee87887f
3 changed files with 30 additions and 4 deletions
-3
View File
@@ -1,3 +0,0 @@
MONGO_CONNECTION_STRING=mongodb+srv://default:M9iZXokJlZpN4KLX@cluster0.mkuqa.mongodb.net/default?retryWrites=true&w=majority
JWT_TOKEN_SECRET=afajdslfwk1@lkkasdfl21ASDF!2
+4 -1
View File
@@ -4,6 +4,7 @@ import * as Tracing from '@sentry/tracing';
/* eslint-disable @typescript-eslint/no-var-requires */
import express, { Application, NextFunction, Request, Response } from 'express';
import InitializeWs from './src/socket';
import cors from 'cors';
import morgan from 'morgan';
@@ -31,9 +32,11 @@ const server = app.listen(PORT, () =>
// won't catch any errors here and we don't want it to
console.log();
require('socket.io')(server, {
const io = require('socket.io')(server, {
// todo: add authentication
cors: {
origin: '*',
},
});
InitializeWs(io);
+26
View File
@@ -0,0 +1,26 @@
export default function InitializeWs(io: any) {
console.log('initializing web sockets');
return io
.use(function (socket: any, next: any) {
console.log('authenticating user...');
try {
// const { token } = socket.handshake.query;
// console.log(token);
// verify jwt token with our api secret
// var decoded: JwtClaims = jwt.verify(token, config.JWT_TOKEN_SECRET);
// socket.userInfo = decoded;
next();
} catch (error) {
console.error(error);
next(new Error('WS Authentication Error'));
}
})
.on('connection', (socket: any) => {
console.log(`connected`, socket.id);
});
}