Skip to content

Commit

Permalink
[example] Write 401 response before destroying the socket (websockets…
Browse files Browse the repository at this point in the history
  • Loading branch information
ehpc committed Sep 17, 2020
1 parent 535c556 commit d972c33
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion examples/express-session-parse/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,21 @@ app.delete('/logout', function (request, response) {
});

//
// Create HTTP server by ourselves.
// Create an HTTP server.
//
const server = http.createServer(app);

//
// Create a WebSocket server completely detached from the HTTP server.
//
const wss = new WebSocket.Server({ clientTracking: false, noServer: true });

server.on('upgrade', function (request, socket, head) {
console.log('Parsing session from request...');

sessionParser(request, {}, () => {
if (!request.session.userId) {
socket.write('HTTP/1.1 401 Unauthorized\r\n\r\n');
socket.destroy();
return;
}
Expand Down

0 comments on commit d972c33

Please sign in to comment.