Websocket angular and php

0

Hello, I have a websocket in php and a factory in angular. I need to know, when there is, or not, connection with websocket, in the code part send, socket.send (JSON.stringify (data)); of this factory

.factory('socket', [function ($rootScope) {
var wsUri = "ws://162.243.172.112:1234";//localhost:1234
try{
    var socket = new WebSocket(wsUri);
    }catch(e){
        socket.onerror = function(ev){
            console.log(ev);
            alert("Falha");
        }
    }


return {
    send: function (data) {
        socket.send(JSON.stringify(data));

    },
    message: function(callback){
        socket.onmessage = function(ev) {
            var result = JSON.parse(ev.data); //PHP sends Json data
            //console.log(result);
            callback(result);

            $rootScope.$apply(function () {
                $rootScope.mensagens=result;
            });

        };
    },

    error: function(callback){
        socket.onerror = function(ev){
            console.log(ev);
            alert("Falha");
        }
    }

};

}])

    
asked by anonymous 05.10.2016 / 15:45

0 answers