0

have angularjs app with few controllers.Every controller need to connect with server to receive some data. I use websocket connection. First I did next: created websocket connection in every controller:

 ws = new WebSocket(wsUrl);

I used the same wsUrl for all. But the problem is that when second connection opens, the first one doesn't response. Than I've read that better way to make one connection and use it from diferent controllers.

the question is - how to make it? how to use websocket.onmessage from different controllers?

ws.onmessage = function (evt) {
            var data = angular.fromJson(evt.data);
            if (data.ok == false) {
                console.log('Error: {ok:false}') ;
            } else if (data.content.type != 4) {
                switch (data.state) {
                    case 'get_work_iterations':
                        listWorkIterations(data); // need to be started in controller_1

                        break;
                    case 'updateIterations':
                        updateIterations(data); //need to be started in controller_2

                }
            }
        };
Serhiy
  • 1,893
  • 9
  • 30
  • 48

1 Answers1

0

I found the way: http://clintberry.com/2013/angular-js-websocket-service/ with services and callbacks

Serhiy
  • 1,893
  • 9
  • 30
  • 48