I have trouble in refactoring the following code block into separate functions.
socketHandler = (io) => {
io.on('connection', (socket) => {
socket.on('doLogin', data => {
userService.getUserByName(data.uname)
.then((doc) =>{
if(doc && doc.pwd===data.pwd){
socket.emit('onLogin', {status:'SUCCESS'});
}
}, (error) => {
socket.emit('onLogin', {status:'Error in the application'});
});
});
});
}
app.configure(socketio(socketHandler));
I tried refactoring the above code as follows.
doLogin = data => {
userService.getUserByName(data.uname)
.then((doc) =>{
if(doc && doc.pwd===data.pwd){
socket.emit('onLogin', {status:'SUCCESS'});
}
}, (error) => {
socket.emit('onLogin', {status:'Error in the application'});
});
}
socketHandler = (io) => {
io.on('connection', (socket) => {
socket.on('doLogin', doLogin);
});
}
app.configure(socketio(socketHandler));
I am getting a run-time error as socket is not defined.
How to get reference to 'socket' in the function 'doLogin'?
I also tried the following way and could not make it work.
doLogin = socket => data => {
Also tried as follows
socket.on('doLogin', doLogin.bind(socket));
Need some help in fixing this.
Thanks.