{"size": 2204, "ext": "js", "max_stars_repo_path": "TestHtml/test.js", "max_stars_repo_name": "senwa/MyCSharpWebSocketServer", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "TestHtml/test.js", "max_issues_repo_name": "senwa/MyCSharpWebSocketServer", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "TestHtml/test.js", "max_forks_repo_name": "senwa/MyCSharpWebSocketServer", "max_forks_repo_forks_event_min_datetime": "2020-04-23T08:15:38.000Z", "max_forks_repo_forks_event_max_datetime": "2020-04-23T08:15:38.000Z"}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 19.8558558559, "max_line_length": 60, "alphanum_fraction": 0.4786751361}
﻿function Sock(url) {
    this.url = url;
    this.sock = null;
    this.connected = false;
    this.timeout = null;
    this.receive = null;
    
    this.reset();
    this.open();
}

Sock.prototype = {
    open: function () {
        var me = this;

        if (me.sock) {
            me.sock.onclose = null;
            me.sock.close();
        }

        var sock = me.sock = new WebSocket(me.url);

        sock.onopen = function (e) {
            me.reset();
            me.connected = true;
            me.onopen && me.onopen(e);
        };

        sock.onerror = function (e) {
			console.log(e);
            sock.close();
        }

        sock.onclose = function (e) {
            me.connected = false;
            me.retry && me.retry();
            me.onclose && me.onclose(e);
        }

        sock.onmessage = function (evt) {
			console.log(evt);
            me.onmessage && me.onmessage(evt.data);
        }
    },

    send: function (obj) {
        this.sock.send(JSON.stringify(obj));
    },

    close: function () {
        this.retry = null;
        this.sock && this.sock.close();
    },

    reset: function () {
        this.retry = this._retryLoop;
        this.retryMs = 500;
    },

    _retryLoop: function () {
        var me = this;
        window.clearTimeout(me.timeout);
        me.timeout = window.setTimeout(function () {
            me.retryMs = Math.min(15000, me.retryMs += 500);
            me.open();
        }, me.retryMs);
    }
};


$(function () {

    function status(msg) {
        var d = $('<div />');
        d.text(msg);
        $('#status').append(d);
    }

    var app = new Sock('ws://localhost:8881');
    
	console.info(app);
	
    app.sock.onopen = function () {
        $('#status').html(' ');
        status('Ready');
    };

    app.sock.onclose = function () {
        status('Closed');
    };
	
	app.sock.onmessage = function (evt) {
        status(evt.data);
		console.log(evt.data);
    };
	
    $('#sendBtn').bind('click',function () {
        var message = $('#message').val();
		var msgList=[{
			ID:"1",
			PrintCode:message
		}];
		
		var msg = 
		{		
			msgs:msgList
		};
		
        app.sock.send(JSON.stringify(msg));
    })
});