B4J Question [ b4j_ws.js ] how do I get information if the server is disconnected?

Waldemar Lima

Well-Known Member
Licensed User
Longtime User
Hello guys, I'm using b4j_ws.js with JServer, does anyone know how to tell me how I know the server was disconnected or connected to js?
 

aeric

Expert
Licensed User
Longtime User
Check this:
 
Upvote 0

Waldemar Lima

Well-Known Member
Licensed User
Longtime User
but how do i know if you disconnected, an error occurred and connected inside b4j_ws.js?
 
Last edited:
Upvote 0

Waldemar Lima

Well-Known Member
Licensed User
Longtime User
If you checked the video posted by Erel, you should noticed that a message popped out saying that "the server is not available".


I read some topics about JavaScript on google, and I got this result :


b4j_ws.js :
JavaScript:
//B4J WebSockets client library v0.92 edited



/*jslint browser: true*/

/*global $, jQuery, WebSocket*/

/*jshint curly: false */

"use strict";

var b4j_ws;

var b4j_closeMessage = false;

//only called as a result of a server request that is waiting for result.

//this method should not be called in any other case.

function b4j_sendData(data) {

    b4j_ws.send(JSON.stringify({type: "data", data: data}));

}

function b4j_raiseEvent(eventName, parameters) {

    try {

        if (b4j_ws.readyState !== 1) {

            if (b4j_closeMessage === false) {

                window.console.error("connection is closed.");

                if (typeof ReconnectingWebSocket !== 'undefined')

                    window.alert("Server is currently not available.");

                else

                    window.alert("Connection is closed. Please refresh the page to reconnect.");

                b4j_closeMessage = true;

            }

        } else {

            b4j_closeMessage = false;

            b4j_ws.send(JSON.stringify({type: "event", event: eventName, params: parameters}));

        }

    } catch (e) {

        window.console.error(e);

    }

}

function b4j_addEvent(selector, event, eventName, preventDefault) {

    var obj = $(selector);

    if (obj.length > 0) {

        obj.on(event, function (e) {

            if (preventDefault) {

                e.preventDefault();

                e.stopPropagation();

            }

            b4j_raiseEvent(eventName, {which: e.which, target: e.target.id, pageX: e.pageX, pageY: e.pageY, metaKey: e.metaKey});

        });

    }

}

function b4j_addAutomaticEvents(data) {

    $.each(data, function (index, value) {

        b4j_addEvent("#" + value.id, value.event, value.id + "_" + value.event, true);

    });

}

function b4j_runFunction(func, params) {

    return window[func].apply(null, params);

}



function b4j_eval(params, script) {

    var f = new Function(script);

    return f.apply(null, params);

}



function b4j_connect(absolutePath,callopenfunction,callclosefunction,callerrorfunction) {

    if (typeof WebSocket === 'undefined') {

        window.alert("WebSockets are not supported by your browser.");

        return;

    }

    var l = window.location, fullpath;

    fullpath = ((l.protocol === "https:") ? "wss://" : "ws://") + l.hostname + ":" + l.port + absolutePath;

    if (typeof ReconnectingWebSocket === 'undefined') {

        b4j_ws = new WebSocket(fullpath);

    } else {

        b4j_ws = new ReconnectingWebSocket(fullpath);

    b4j_ws.onmessage = function (event) {

        var ed = JSON.parse(event.data);

        if (ed.etype === "runmethod") {

            $(ed.id)[ed.method].apply($(ed.id), ed.params);

        } else if (ed.etype === "runmethodWithResult") {

            b4j_sendData($(ed.id)[ed.method].apply($(ed.id), ed.params));

        } else if (ed.etype === "setAutomaticEvents") {

            b4j_addAutomaticEvents(ed.data);

        } else if (ed.etype === "runFunction") {

            b4j_runFunction(ed.prop, ed.value);

        } else if (ed.etype === "runFunctionWithResult") {

            b4j_sendData(b4j_runFunction(ed.prop, ed.value));

        } else if (ed.etype === "eval") {

            b4j_eval(ed.value, ed.prop);

        } else if (ed.etype === "evalWithResult") {

            b4j_sendData(b4j_eval(ed.value, ed.prop));

        } else if (ed.etype === "alert") {

            window.alert(ed.prop);

        }

      

    };

    //  I needed to know information about events and call a function from another file.js to display connection error information to my users ...

    b4j_ws.onopen  = function (event) {

  

        callopenfunction(event.data);



    };



    b4j_ws.onclose   = function (event) {

  

        callclosefunction(event.data);



    };

  

    b4j_ws.onerror    = function (event) {

  

        callerrorfunction(event.data);



    };



    } 

}



I did it in a hurry ... For those who want to improve the code above, it will be a pleasure! :)
 

Attachments

  • b4j_ws.zip
    5.4 KB · Views: 234
Upvote 0
Top