B4J Question with websocketclient reading remote websocketserver

jinyistudio

Well-Known Member
Licensed User
Longtime User
Hi,

my websocketclient(HTML file) on "A Server", My websocketserver(B4J + MySQL) on "B Server". How could i read some data from websocketserver with websocketclient in the HTML file ? It is OK with b4j_connect("192.168.1.200/ws") ?


I know it is Ok when they are same server. :)
 
Last edited:

jinyistudio

Well-Known Member
Licensed User
Longtime User
I add a new parameter(portn,port number for websocketserver) to b4j_connect1. It is OK ;)

B4X:
unction b4j_connect1(portn,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 + ":" + portn + absolutePath;
    b4j_ws = new WebSocket(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);
        }
      
    };
}
 
Upvote 0

jinyistudio

Well-Known Member
Licensed User
Longtime User
Not sure why you need it. You can already pass the port: https://b4x.com:51041/

Your code looks fine.
I have try your example. With b4j_connect() function in my html side cann't connect to WebsocketServer(B4J). I get a error when i monitor it. the host fullpath have missing a "port number". beacause the websocketserver need a "port number". so i add b4j_connect1(). :p bad english !

My html path = shop.jinyistudio.tw/easycat/001/easycat_hat.html

my Websocketserver = shop.jinyistudio.tw:8008/ws/stock --> b4j_connect --> shop.jinyistudio.tw/ws/stock
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
My guess here is that you are using a non-B4J web server to host your HTML files and B4J to do the host the WebSocket server?
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Now I get your solution. BTW, if you want to connect to a WebSocket server that is not on the same machine, then you'll have to create a b4j_connect method that takes fullpath as the parameter. You then are responsible for correctly selecting ws://,wss://, hostname, port (if applicable) and path information. The original b4j_connect method builds everything for you except the absolutePath. With your implementation, the programmer is also responsible for the proper port assignment (which, if changed, will break all connections).
 
Upvote 0

jinyistudio

Well-Known Member
Licensed User
Longtime User
Yes! beacuse my case is all in same host(shop.jinyistudio.tw). so i just need add port number. :)
 
Upvote 0
Top