/**
 * Created by cc on 3/09/15.
 */
var socketApp = angular.module('myApp.socketApp',[]);


socketApp.factory('MyData', function($websocket) {
    // Open a WebSocket connection
    var ws=$websocket('ws://localhost:6881/forward');

    ws.onOpen(function() {
        console.log('connection open');
    });

    ws.onError(function(event) {
        console.log('connection Error', event);
    });

    ws.onClose(function(event) {
        console.log('connection closed', event);
    });
    // setTimeout(function() {
    //   ws.close();
    // }, 500)


    var collection = [];


    return {

        collection: collection,
        status: function() {
            return ws.readyState;
        },
        send: function(message) {
            if (angular.isString(message)) {
                ws.send(message);
            }
            else if (angular.isObject(message)) {
                ws.send(JSON.stringify(message));
            }
        },
        ws: ws

    };
});
