<commit_before>window.SocketManager = Ember.Object.extend({

	listeners : {},
	socket : null,
    connection : false,
    reconnect : false,
    timer : null,

    initConnection : function() {
        this.set("reconnect", true);
        this.connect();
    },
	
	connect : function() {
        if (this.get('connection') || !this.get("reconnect"))
            return;

        var self = this;

        var url = this.adapter.linkLibrary["datasocket"];
        url = url.replace(/https?/, "ws");

        var s = new WebSocket(url + "?authToken=" + this.authManager.token());
        this.set('socket', s);
        this.set('connection', true);

        if (s.readyState == 3 || s.readyState == 4) {
            this.onClose(); // Close socket immediately if connection failed
            return;
        } else {
            self.onMessage({data:'{"type": "notification","value": {"action" : "clear"}}'},self);
        }

        s.onClose = this.onClose;
        s.onmessage = function(event) {self.onMessage(event,self);};

        this.set('timer', setInterval(function() {
            var s = self.get('socket');
            if (s.readyState == 3 || s.readyState == 4) {
                s.onClose();
            }
        }, 1000));
    }.observes("connection"),

    onClose : function() {
        this.onMessage({data:'{"type": "notification","value": {"message" : "lost connection with server. Trying to reconnect..."}}'},this);

        var timer = this.get('time');
        if (timer) {
            clearInterval(timer);
        }
        this.set('connection', false);
    },

    disconnect : function() {
        clearInterval(this.get('timer'));
        this.get("socket").close();
        this.set("reconnect", false);
        this.set("connection", false);
    },
	
	onMessage : function(event, self) {
		var jsonData = $.parseJSON(event.data);
		if (self.listeners[jsonData.type]) {
			var callbacks = self.listeners[jsonData.type][0] || [];
			if (jsonData.id && self.listeners[jsonData.type][jsonData.id]) {
				callbacks.pushObjects(self.listeners[jsonData.type][jsonData.id]);
			}
			
			$.each(callbacks, function(index, callback) {<commit_msg>fixing connection issue with socket<commit_after>window.SocketManager = Ember.Object.extend({

	listeners : {},
	socket : null,
    connection : false,
    reconnect : false,
    timer : null,

    initConnection : function() {
        this.set("reconnect", true);
        this.connect();
    },
	
	connect : function() {
        if (this.get('connection') || !this.get("reconnect"))
            return;

        var self = this;

        var url = this.adapter.linkLibrary["datasocket"];
        url = url.replace(/https?/, "ws");

        this.set('connection', true);
        var s = new WebSocket(url + "?authToken=" + this.authManager.token());
        self.set("socket", s);

        /*if (s.readyState == 2 || s.readyState == 3) {
            this.onClose(self); // Close socket immediately if connection failed
            return;
        }*/

        s.onclose = function() { self.onClose(self) };
        s.onopen = function() { self.onOpen(self); };
        s.onmessage = function(event) {self.onMessage(event,self);};
    }.observes("connection"),

    onClose : function(self) {
        self.onMessage({data:'{"type": "notification","value": {"message" : "lost connection with server. Trying to reconnect..."}}'},this);

        setTimeout(function() {
            self.set('connection', false);
        }, 1000);
    },

    onOpen : function(self) {
        self.onMessage({data:'{"type": "notification","value": {"action" : "clear"}}'},self);

        /*self.set('timer', setInterval(function() {
            var s = self.get('socket');
            if (s.readyState == 2 || s.readyState == 3) {
                s.onclose();
            }
        }, 1000));*/
    },

    disconnect : function() {
        clearInterval(this.get('timer'));
        this.get("socket").close();
        this.set("reconnect", false);
        this.set("connection", false);
    },
	
	onMessage : function(event, self) {
        console.log(event.data);
		var jsonData = $.parseJSON(event.data);
		if (self.listeners[jsonData.type]) {
			var callbacks = self.listeners[jsonData.type][0] || [];
			if (jsonData.id && self.listeners[jsonData.type][jsonData.id]) {
				callbacks.pushObjects(self.listeners[jsonData.type][jsonData.id]);
			}
			
			$.each(callbacks, function(index, callback) {
{'fraction_non_alphanumeric': 0.1752021563342318, 'fraction_numerical': 0.004941599281221923, 'mean_word_length': 2.462674961119751, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2086879', 'n_tokens_mistral': 1391, 'n_tokens_neox': 1298, 'n_words': 252}