define('jForumSocket', [
    'require',
    'jquery',
    'SockJS'
], function (require, $, SockJS) {
    'use strict';

    var jForumSocket = (function () {
        class ForumSocket {
            constructor(reqUri, jqEle, option) {
                this.sock = new SockJS(reqUri);
                this.requestDataFun = option.sendDataFun || null;
                this.messageCallback = option.callbackFun || null;
                this.jqEle = jqEle;
            }
            init(name) {
                var t = name || '20201005';
                var _mcf = this.messageCallback;
                this.sock.onopen = function () {
                    console.log(t + ' Socket opened!');
                };

                this.sock.onmessage = function (e) {
                    if ($.isFunction(_mcf)) {
                        _mcf(e.data);
                    }
                };

                this.sock.onclose = function () {
                    console.log(t + ' Socket close');
                };
                this.sock.onerror = function (e) {
                    console.log(t + ' Socket has Error: ' + e);
                };
            }
            _send() {
                if (!$.isFunction(this.requestDataFun)) {
                    return;
                }
                var jsonData = this.requestDataFun(this.jqEle);
                if ($.isEmptyObject(jsonData)) {
                    return;
                }
                var t = JSON.stringify(jsonData);
                this.sock.send(t); //消息数量,
            }
            start(loopUnit) {
                var _curIns = this;
                setInterval(function () {
                    _curIns._send();
                }, loopUnit); //毫秒
            }
        }
        return ForumSocket;
    })();
    return jForumSocket;
});