B4J Question Get Portnumber from Websocket

Swissmade

Well-Known Member
Licensed User
Longtime User
Hi all,

Can antibody tell me how to get the port-number from a web-socket connected with the Jetty Server.

Many thanks for help.
 

Swissmade

Well-Known Member
Licensed User
Longtime User
Get FullRequestURI and parse it.
B4X:
    Log(ws.UpgradeRequest.FullRequestURI)   'http://localhost:51042/ws

This I have already ok seems too be the only way.
Thanks Erel
 
Upvote 0

Swissmade

Well-Known Member
Licensed User
Longtime User
Solved
Thanks all for help.
 
Upvote 0

Swissmade

Well-Known Member
Licensed User
Longtime User
Not my credit this time...

Another solution:
B4X:
Dim p As Int
If ws.Secure Then p = Main.srvr.PortSSL Else p = Main.srvr.Port

This is not possible because the Server is a array srvr(10)
 
Upvote 0

Swissmade

Well-Known Member
Licensed User
Longtime User
Yes Erel,

When I read your question I think that this is the wrong way to do this.
I need a Server is running many Websockets to connect to different locations.

Maybe there is a better way to do this.
Any help will be nice.
 
Upvote 0

Swissmade

Well-Known Member
Licensed User
Longtime User
I will try to explain it more.

We have IO modules to control some gates. This Gates are on different locations.
So IP and portnumber can be different.
The info I get from the modules has to go to one webpage in frames.
One frame per module.
So this will result in a page of 10 or more small status and control windows.

Maybe this helps to better understand what I like to do.
Below the code where I define the Server array.
B4X:
Dim srvr(ServerArraySize) As Server
    For ServerCount = 0 To ServerArraySize -1
        srvr(ServerCount).Initialize("srvr")
        srvr(ServerCount).AddWebSocket("/ws", "modETH484")
        srvr(ServerCount).LogWaitingMessages = False
        srvr(ServerCount).LogsRetainDays = 5
        srvr(ServerCount).Port = ServerPortStart + ServerCount
        Log ("Server at port " & srvr(ServerCount).Port)
'    Log(srvr.CurrentThreadIndex)
        srvr(ServerCount).LogWaitingMessages = False
        srvr(ServerCount).Start
    Next
 
Upvote 0

Swissmade

Well-Known Member
Licensed User
Longtime User
Yes and that's the problem.
Then i need something to see where the IOModule is connected.
And this I don't know how to do.
Any idea Erel.

I use a javascript in the site see code
Maybe some parameters here.

B4X:
<script>
    //connect to the web socket when the page is ready.
    $( document ).ready(function() {
        b4j_connect("/ws");
// The confirm dialog. Ensure the selector and the b4j raise events naming are exactly matching in the B4J code
    $( "#confirmdialog" ).dialog({
          resizable: false,
           height:180,
           modal: true,
           autoOpen: false,
           buttons: {
         "Ja": function()
             {
               b4j_raiseEvent("confirmdialog_event", {"result": "yes"});
               $( this ).dialog( "close" );
             },
         "Nee": function()
             {
              b4j_raiseEvent("confirmdialog_event", {"result": "no"});
               $( this ).dialog( "close" );
             }
          }
         });

        $( "#alertdialog" ).dialog({
           resizable: false,
           modal: true,
           height:150,
           width: 180,
           autoOpen: false,
           buttons: {
           "Close": function()
             {
               b4j_raiseEvent("alertdialog_event", {"result": "close"});
               $( this ).dialog( "close" );
             }
         }
       });
    });
    </script>
 
Upvote 0

Swissmade

Well-Known Member
Licensed User
Longtime User
The IOModules I can send http requests to get the status.
Also to send an command to a module I can use TCP/IP message or HTTP.
The Modules can not work with websockets.
All information from the IOModule is a request from the Server WS Part.

The website is connect to ws where I send the information to.
Also the Command's from buttons is send via ws and in the server there will be build the right command to send to the Module.
As I told there will be about 10 IO Modules on different locations in a VPN network.
The Website is build with frames where I put the Page for the modules in.
So we will have about 10 or more frames in the site.
Frames are small so this will fit on one page.

Hope this helps.
Thanks Erel.

ps
I also have try http://192.168.1.10:8888?port=8888
But the Port parameter is lost when I connect
 
Last edited:
Upvote 0

Swissmade

Well-Known Member
Licensed User
Longtime User
Creating multiple servers is not the correct solution.

What are the clients? Android devices?

You can send any information you need between the client and the server after the websocket is connected.

The device are PC's in a Control room. No need for Devices.

Pc Clients not more then two.
 
Last edited:
Upvote 0

billzhan

Active Member
Licensed User
Longtime User
I guess the system works this way. If it's right, what's your issue ? Why use srvr(10) , it can be done with one srvr (and one ws module)?

IO modules (IO#1 IO#2 ) ETH484#1 / ETH484#2 ... --- in different locations which can be accessed by their ip addresses

Device (PC in control room with B4J app) --- B4J app sends HTTP requests to IO modules get/set IO module status, and work as a web page/websocket server )

Monitor (PCs which are used to access B4J server) --- staff get/set IO modules

IO modules <--- (http get)--- Device (B4J) <---(websocket)--> Monitor

In the web page you select :
1. IO module number (to get ip address)
2. which command to send to IO module
3. send command to IO module(http://ip?command)
4. read results (if any) and display in web page(s)
 
Last edited:
Upvote 0
Top