B4J Question Websocked and jserver

besoft

Active Member
Licensed User
Longtime User
Hi, I need help.
Can anyone provide some example of jserver and websocket.
I am trying to send JSON data to a table and when I click on the table I get back the data from the row.
I would be very grateful.

I've tried so many examples and even AI didn't help much, I don't even have any code at the moment because I've completely lost track...
THANK YOU
 
Solution
Have you set an id for the table element in your webpage? Have you declared it as a jqueryelement in the class_globals sub of your websocket class? That is the key to be able to interact with the elements that you have on your webpage. After doing so, you may also want to add the events for that jquery element such as click if you need to.

Let's say you have in your html file a div with the id "maindiv"
HTML:
<div id="maindiv">
   
</div>

Now, in your Websocket Class you have to declare it in the Sub Class_Globals
B4X:
Sub Class_Globals
        Private ws As WebSocket
        Private maindiv As JQueryElement '<--- This will allow you to interact with your element.
End Sub

After that you can access all the jQueryElement methods to...

aeric

Expert
Licensed User
Longtime User
Have you already go through the server examples?

If you want to send/receive JSON data, I have solution not using websocket but instead using Web API.
 
Upvote 0

besoft

Active Member
Licensed User
Longtime User
I'm still struggling with the project:

I get the following data from the page(I can see it in the console on the page):

Object { type: "RowClick", value: {...} }
type: "RowClick"
value: Object { priority: "0", stevdn: "787891", sifra_mp: "22221", ... }
constructed_round: "30000. 0000"
launched_kol: "70000.0000"
location_skl: "OSN"
title: "M NODE CONTACT IT "
priority: "0"
sifra_mp: "22221"
sifra_skl: "202"
sm: "1241-IPO"
status: "LA"
stev_dn: "787891"

how do i capture this and display it in the log ?

Please help me if anyone can help.
 
Upvote 0

Fernando Solá

Member
Licensed User
Longtime User
Have you set an id for the table element in your webpage? Have you declared it as a jqueryelement in the class_globals sub of your websocket class? That is the key to be able to interact with the elements that you have on your webpage. After doing so, you may also want to add the events for that jquery element such as click if you need to.

Let's say you have in your html file a div with the id "maindiv"
HTML:
<div id="maindiv">
   
</div>

Now, in your Websocket Class you have to declare it in the Sub Class_Globals
B4X:
Sub Class_Globals
        Private ws As WebSocket
        Private maindiv As JQueryElement '<--- This will allow you to interact with your element.
End Sub

After that you can access all the jQueryElement methods to interact with your element. Let's say I want to add a text inside that div:
B4X:
maindiv.SetText("This is a sample text")

In some cases you may need to flush your Websocket object... it depends if the action is a response to an event started from the client (the browser in this case) or if it is the result from some other code in your B4X project... like updating some data in your webpage as it becomes available. In this last case you would need to flush the Websocket.
ws:
ws.Flush '<--- This was also declared in the Sub Class_Globals and it was asigned to the websocket of the client when it connected on the WebSocket_Connected event.

Now, let's suppose you want to READ that information. You would need to get the value from the future object that would be returned when you query the element. For instance, let's log it.
B4X:
    Dim future_Text As Future = maindiv.GetText()
    Log(future_Text.Value)

In a nutshell is all about using jQueryElement objects and Future objects. The rest is good old B4X. Check the sample project that comes with the template, it really helps. I hope this helps you get started. Good Luck!
 
Last edited:
Upvote 0
Solution

besoft

Active Member
Licensed User
Longtime User
Thank you for your reply and help, unfortunately I can't do the test anymore today because I'm not at work. But I will tomorrow and report back.

;)
 
Upvote 0

besoft

Active Member
Licensed User
Longtime User
One big thank you to Mr Fernando Solá for helping me solve the problem. I learned a lot of things ..
🙂
 
Upvote 0
Top