[Closed]b4j websocket jquery

monic

Active Member
Licensed User
Longtime User
Hi im looking for help with developing a php script that can connect to a b4j websocket server and send data.

This is the thread that was downloaded https://www.b4x.com/android/forum/threads/custom-websocket-based-push-framework.40272/ I want to change the client jquery facing push sample to a php client page that doesn't use a textbox or button to send the data to the connected client

:)

this is the script that needs to be converting

B4X:
//B4J WebSockets client library v0.9

/*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.
//inject to data from php
function b4j_sendData(data) {
    b4j_ws.send(JSON.stringify({type: "data", data: data}));
  console.log(data);
}
function b4j_raiseEvent(eventName, parameters) {
    try {
        if (b4j_ws.readyState !== 1) {
            if (b4j_closeMessage === false) {
                window.console.error("connection is closed.");
                window.alert("Connection is closed. Please refresh the page to reconnect.");
                b4j_closeMessage = true;
            }
        } else {
            b4j_ws.send(JSON.stringify({type: "event", event: eventName, params: parameters}));
         
          // console.log(parameters);
          console.log("debug =================================");
          //console.log(eventName);
        }
    } 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);
      console.log(index);
    });
}
function b4j_runFunction(func, params) {
    return window[func].apply(null, params);
  console.log(func);
}

function b4j_eval(params, script) {
    var f = new Function(script);
  console.log(script);
    return f.apply(null, params);
}

function b4j_connect(absolutePath) {
  console.log(absolutePath);
    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;
    b4j_ws = new WebSocket(fullpath);
    b4j_ws.onmessage = function (event) {
        var ed = JSON.parse(event.data);
      console.log(ed);
        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);
        }
       
    };
}
 

DonManfred

Expert
Licensed User
Longtime User

monic

Active Member
Licensed User
Longtime User
I'm having trouble with the exchange of the correct formation of data that needs to be supplied to the websocket server in order to send

B4X:
{"value":["#tblmessages",[["287","test"],["286","test"],["285","3"],["284","999"],["283","999"],["282","999"],["281","999"],["280","''"],["279","\\["],["278","\\"]]],"prop":"$(arguments[0]).dataTable().fnAddData(arguments[1])","etype":"eval"}    250   
05:55:16.573
{"id":"#users","method":"html","params":["<li>Total number of users: 2<\/li>"],"etype":"runmethod"}    99   
05:55:16.606
{"type":"event","event":"btnsend_click","params":{"which":1,"target":"btnsend","pageX":197,"pageY":101,"metaKey":false}}    120   
05:55:43.758
{"id":"#txt","method":"val","etype":"runmethodWithResult"}    58   
05:55:43.780
{"type":"data","data":"test"}    29   
05:55:43.788
{"id":"#txt","method":"select","etype":"runmethod"}    51   
05:55:43.808
{"value":["#tblmessages"],"prop":"$(arguments[0]).dataTable().fnClearTable()","etype":"eval"}    93   
05:55:43.848
{"value":["#tblmessages",[["288","test"],["287","test"],["286","test"],["285","3"],["284","999"],["283","999"],["282","999"],["281","999"],["280","&#39;&#39;"],["279","\\["]]],"prop":"$(arguments[0]).dataTable().fnAddData(arguments[1])","etype":"eval"}    252   
05:55:43.878

1
{"value":["#tblmessages",[["288","test"],["287","test"],["286","test"],["285","3"],["284","999"],["283","999"],["282","999"],["281","999"],["280","&#39;&#39;"],["279","\\["]]],"prop":"$(arguments[0]).dataTable().fnAddData(arguments[1])","etype":"eval"}
 

b4xscripter

Member
Licensed User
Longtime User
Can I help you? I use the node.js for the socket communication with Android phone. Regards
 

monic

Active Member
Licensed User
Longtime User
Hi,
I've been experimenting with jquery in the browser but i would like a headless version that doesn't depend on the browser being open to relay between phone. :)
 
Last edited:
Top