B4J Question Websockets and Parameters

Eric Baker

Member
Licensed User
Longtime User
I am playing around with B4J Websocket programming. (Trying a multipage app, using WebAppHelloWorld as the basis)

How can I pass parameters (either GET or POST) to my my handler?

I tried a simple get request:

B4X:
http://192.168.1.106:51042/?a=1&b=2

and tried to extract the search query from Websocket.UpgradeRequest.ParameterMap, only to receive an empty map.

Looking at b4j_ws.js I saw that the search query is not passed on to the web socket.

The original line in b4j_ws.js reads:

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

Which I modified to include the search query

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

Now the search query gets passed on to the handler, but if I look at the ParameterMap, it contains the proper keys, but the values are java.lang.String objects.

B4X:
'result of Log(WebSocket1.UpgradeRequest.ParameterMap)
(MyMap) {b=[Ljava.lang.String;@169e486, a=[Ljava.lang.String;@184a0b7}

I have no idea what to with these in B4J.

Questions:
Is there another way of passing query parameters without modifying b4j_ws.js?
If I do modify it, how do I handle the java.lang.String?
Can I pass the parameters of a POST request?
Am I doing it all wrong and should go hide in a corner?
 

Eric Baker

Member
Licensed User
Longtime User
The values you see are arrays of strings.

Thank you. That helped and I was able to extract the parameters

I don't recommend you to modify the javascript code.

I agree. I'd rather not.

Where do these parameters come from?

They were passed via URL as a search query, e.g.. http://localhost:51042/?a=1&b=2

There are other ways to pass data to the handler.

Yes, that was part of my question. How can I go from one webpage using web sockets to another webpage using web sockets and pass a parameter. (GET or POST) Example, go from a page with a list, and when clicking on a list item, go to a detail page.
 
Last edited:
Upvote 0

Eric Baker

Member
Licensed User
Longtime User

Thank you very much. I overlooked the chat example not thinking it would be a two page app. This should do the trick in my use case.

Nevertheless, I think it would still be useful to be able to pass parameters via URL to allow deep linking into the app (e.g. sending a link via e-mail to a product page, or to allow search engine friendly URLs). Right now, I don't see how it can be done using the b4j_ws script as is... unless i create another script to build a parameter for the b4j_connect function in the html file.

I think I just answered my own question. ;)

Would be nice though, if b4j_ws had that functionality built in, allowing you to pass GET and POST parameters to the web socket. I'll try and see if I can implement that somehow.

Thank you very much again, Erel
 
Upvote 0
Top