-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Socket.io #23
base: main
Are you sure you want to change the base?
Conversation
RhythmAgg
commented
Oct 19, 2023
- Multi user socket.io connection
- Authentication middleware changed
- Socket Messages send on interval of 10 files uploaded
…ub.com/UmangSharma5/DFS_ReactViewer into misc/#22/Refactor-Code-Routes-Handling
client/src/App.js
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whys is this file changed. Make no changes in frontend code
client/package.json
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
revert this change for now
server/routes/objects.js
Outdated
@@ -12,6 +12,7 @@ import { walk } from "walk"; | |||
import { map_user_to_bucket, get_user_bucket, remove_user_bucket, map_file_type, file_stats,file_uploaded } from '../Database_queries/queries.js' | |||
|
|||
const sem = semaphore(100) | |||
let socket = [] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const sockets = []
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better make a socket handler which handles all the active sockets
Make another file socketManager.js
with these functions
addSocketConnection()
removeSocketConnection()
...etc
server/routes/objects.js
Outdated
const updateSocket = (usertoken,clientsocket) => { | ||
const entry = socket.findIndex(usersock => usersock.token == usertoken) | ||
console.log(entry) | ||
if(entry != -1) | ||
socket[entry].sock = clientsocket | ||
else | ||
socket.push({token: usertoken,sock: clientsocket}) | ||
} | ||
const removeSocket = (index) => { | ||
socket.splice(index,1) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move all to the socketManager.js
server/routes/objects.js
Outdated
@@ -25,6 +26,17 @@ let convert_tiff_options = { | |||
logLevel: 1 | |||
}; | |||
|
|||
const updateSocket = (usertoken,clientsocket) => { | |||
const entry = socket.findIndex(usersock => usersock.token == usertoken) | |||
console.log(entry) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove
server/routes/objects.js
Outdated
socket[sock].sock.disconnect() | ||
removeSocket(sock) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
put all this in disconnectSiocket()
server/routes/objects.js
Outdated
export { | ||
router, | ||
updateSocket | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any route file should only export router as default
if you want to export anything else, shift it to helpers/ and export from there
import { fileURLToPath } from 'url'; | ||
import require_auth from './middleware/Auth.js'; | ||
import { Server } from 'socket.io' | ||
const server = http.createServer(app); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why this required
const io = new Server(server, { | ||
cors: { | ||
origin: '*' | ||
}, | ||
}); | ||
|
||
// Add this | ||
// Listen for when the client connects via socket.io-client | ||
io.on('connection', (socket) => { | ||
console.log(`User connected ${socket.id}`); | ||
|
||
socket.on('addUser',(user) => { | ||
updateSocket(user,socket) | ||
}) | ||
|
||
//when disconnect from client | ||
socket.on("disconnect", () => { | ||
console.log("a user disconnected!"); | ||
}); | ||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move this to socketManager
we dont need all this code in app.js which is entry point of Node server
server/server.js
Outdated
}); | ||
}); | ||
|
||
server.listen(5000, function () { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
user process.env || 5000
…s-Handling Misc/#22/refactor code routes handling