Skip to content
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

Wait 'join' event to emit in controller #130

Open
ghost opened this issue Nov 28, 2015 · 0 comments
Open

Wait 'join' event to emit in controller #130

ghost opened this issue Nov 28, 2015 · 0 comments

Comments

@ghost
Copy link

ghost commented Nov 28, 2015

I built socket factory and injected it to one of my controllers.

angular
    .module('app')
    .factory('socket', socket);

socket.$inject = ['socketFactory', 'authenticationFactory', 'SOCKET_EVENT', 'API'];
function socket(socketFactory, authenticationFactory SOCKET_EVENT, API) {

    var socketIO = io.connect(API.SOCKET),
        userInfo = authenticationFactory.getUserInfo();

    socketIO.on('connect', function() {
        if(!userInfo) {
            logger.error('No userInfo,logging out!');
            authenticationFactory.logout();
            return;
        }

        socketIO.emit('join', { token: userInfo.token });
    });

    socketIO.on('join', function(data) {
        socketIO.emit(SOCKET_EVENT.REQ_MAIN_DATA, { });
    });

    var mySocket = socketFactory({
        ioSocket: socketIO
    });

    return mySocket;
}

Controller:

angular
    .module('app')
    .controller('Controller', Controller);

Controller.$inject = ['socket', 'SOCKET_EVENT'];
function Controller(socket, SOCKET_EVENT) {
    var vm = this;
    socket.emit(SOCKET_EVENT.REQ_OTHER_DATA);
}

The problem is that when I refresh the view where this controller is located, controller fires socket.emit before join event. So how to wait join event's result in controller?

@ghost ghost changed the title Wait until socket Wait 'join' event to emit in controller Nov 28, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

0 participants