refactor: socket connection hooks

This commit is contained in:
cha0s 2020-12-05 14:41:52 -06:00
parent 9f132a07ed
commit e91be0670f
3 changed files with 8 additions and 14 deletions

View File

@ -1,17 +1,11 @@
import {Class, compose, EventEmitter} from '@latus/core';
import SocketIoServer from 'socket.io';
import ServerSocket from './socket';
import SocketIoParser from './packet/socket.io-parser';
const decorate = compose(
EventEmitter,
);
export default class SocketServer extends decorate(Class) {
export default class SocketServer {
constructor(httpServer, latus) {
super();
// eslint-disable-next-line no-param-reassign
latus.config['%sockets'] = this;
this.onConnect = this.onConnect.bind(this);
@ -30,7 +24,7 @@ export default class SocketServer extends decorate(Class) {
});
this.io.on('connect', this.onConnect);
this.io.use((socket, next) => {
latus.invokeMiddleware('@latus/socket/connection', socket, next);
latus.invokeMiddleware('@latus/socket/authenticate', socket, next);
});
const Packets = latus.invokeReduce('@latus/socket/packets');
// eslint-disable-next-line no-param-reassign
@ -50,7 +44,7 @@ export default class SocketServer extends decorate(Class) {
const serverSocket = new ServerSocket(socket, this.latus);
serverSocket.req.latus = this.latus;
serverSocket.req.adapter = this.io.of('/').adapter;
this.emit('connect', serverSocket);
this.latus.invokeSequential('@latus/socket/connect', serverSocket);
}
send(packet, channel = '/') {

View File

@ -26,10 +26,10 @@ export default {
passport.session()(req, res, next);
});
},
'@latus/socket/connection': () => (socket, next) => {
debug('@latus/socket/connection: passport.initialize()');
'@latus/socket/authenticate': () => (socket, next) => {
debug('@latus/socket/authenticate: passport.initialize()');
passport.initialize()(socket.handshake, undefined, () => {
debug('@latus/socket/connection: passport.session()');
debug('@latus/socket/authenticate: passport.session()');
passport.session()(socket.handshake, undefined, () => {
/* eslint-disable no-param-reassign */
socket.handshake.login = LogOps.logIn;

View File

@ -34,10 +34,10 @@ export default {
});
};
},
'@latus/socket/connection': (latus) => {
'@latus/socket/authenticate': (latus) => {
const fn = socketSession(latus.config['%session']);
return (socket, next) => {
debug('@latus/socket/connection: session()');
debug('@latus/socket/authenticate: session()');
fn(socket, next);
};
},