B4J Question WebSockets in GUI app

Swissmade

Well-Known Member
Licensed User
Longtime User
Hi all,
I wondering if it is possible to use web-sockets in a GUI app.
What I like to do is send the information from a GUI Server also to a website.
If this is working I like to strip down this GUI Server to a Non-GUI Server this way I know if it is working correct.

Also wondering how to do that if it is possible.

Thanks in advance for helping me out here.
 

LucaMs

Expert
Licensed User
Longtime User
java.lang.RuntimeException: StartMessageLoop should only be called in non-UI applications.

So the answer is: NO
 
Upvote 0

Swissmade

Well-Known Member
Licensed User
Longtime User
java.lang.RuntimeException: StartMessageLoop should only be called in non-UI applications.

So the answer is: NO
I was thinking you have the messageloop with the GUI
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I was thinking you have the messageloop with the GUI
I remembered that it was not possible to create websocket servers with GUI but, given @AHilton 's answer, to remove any doubts I tried.
The message clearly states that it is not possible to run StartMessageLoop in projects other than NON-UI.
 
Upvote 0

Swissmade

Well-Known Member
Licensed User
Longtime User
I remembered that it was not possible to create websocket servers with GUI but, given @AHilton 's answer, to remove any doubts I tried.
The message clearly states that it is not possible to run StartMessageLoop in projects other than NON-UI.
Ok then I have to do it an other way.
Many thanks guys for your answering.
 
Upvote 0

AHilton

Active Member
Licensed User
Longtime User
java.lang.RuntimeException: StartMessageLoop should only be called in non-UI applications.

So the answer is: NO

I read the first question : "I wondering if it is possible to use web-sockets in a GUI app" and, of course, you can.

Then, I re-read the second part more closely .... No, you can't create a GUI SERVER websocket.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I read the first question : "I wondering if it is possible to use web-sockets in a GUI app" and, of course, you can.

Then, I re-read the second part more closely .... No, you can't create a GUI SERVER websocket.
Making a mistake (in this case not reading everything right) is Human; that's why I'm never wrong 😄
(because I'm a donkey 😄)
 
Upvote 0

Swissmade

Well-Known Member
Licensed User
Longtime User
Nothing wrong with making mistakes. We learn every day.:):)

Thanks again Guys.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I remembered that it was not possible to create websocket servers with GUI but, given @AHilton 's answer, to remove any doubts I tried.
The message clearly states that it is not possible to run StartMessageLoop in projects other than NON-UI.
You don´t need StatrtMessageLoop
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private cl As WebSocketClient
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
    cl.Initialize("WS")
End Sub
Sub WS_BinaryMessage (Data() As Byte)
    Log($"WS_BinaryMessage(${Data.Length})"$)   
End Sub
Sub WS_Closed (Reason As String)
    Log($"WS_Closed(${Reason})"$)
End Sub
Sub WS_Connected
    Log($"WS_Connected"$)
End Sub
Sub WS_TextMessage(Message As String)
    Log($"WS_TextMessage(${Message})"$)
End Sub
'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub Button1_Click
    cl.Connect("ws://192.168.2.5:51042/ws")
    'xui.MsgboxAsync("Hello world!", "B4X")
End Sub

Snap2.png
 
Upvote 0

AHilton

Active Member
Licensed User
Longtime User
...
What I like to do is send the information from a GUI Server also to a website.
If this is working I like to strip down this GUI Server to a Non-GUI Server this way I know if it is working correct.
...

When I first started with B4J and Websocket solutions, I wanted to do the exact same thing ... create a GUI server so that I'd have better feedback/monitoring of the process. Also, because I was more familiar with creating GUI B4J/B4A/B4I apps and less-so with NON-GUI B4J apps.

After doing a few of those types of solutions, I went to, when creating a new B4J websocket server, doing a simple website interface to it. Even if the final solution had nothing to do with web connectivity. It's a whole lot easier/faster to refresh the display of a website after any modifications to the NON-GUI websocket Server than it is to rebuild the B4A/B4J/B4I app.

Then, several years ago I grew a brain and decided to make a universal (at least to my own projects) websocket connector and (testing) website that would pick up the new server as being there and connect to it automatically. No having to build something custom each time to connect, test, and monitor the development process from the client end. This came as an off-shoot of the universal B4J server monitor project and has greatly increased the productivity.
 
Upvote 0

Swissmade

Well-Known Member
Licensed User
Longtime User
You don´t need StatrtMessageLoop
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private cl As WebSocketClient
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
    cl.Initialize("WS")
End Sub
Sub WS_BinaryMessage (Data() As Byte)
    Log($"WS_BinaryMessage(${Data.Length})"$)  
End Sub
Sub WS_Closed (Reason As String)
    Log($"WS_Closed(${Reason})"$)
End Sub
Sub WS_Connected
    Log($"WS_Connected"$)
End Sub
Sub WS_TextMessage(Message As String)
    Log($"WS_TextMessage(${Message})"$)
End Sub
'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub Button1_Click
    cl.Connect("ws://192.168.2.5:51042/ws")
    'xui.MsgboxAsync("Hello world!", "B4X")
End Sub

View attachment 125034
That's what I was thinking because the messgeloop is from the GUI will check this
 
Upvote 0

Swissmade

Well-Known Member
Licensed User
Longtime User
When I first started with B4J and Websocket solutions, I wanted to do the exact same thing ... create a GUI server so that I'd have better feedback/monitoring of the process. Also, because I was more familiar with creating GUI B4J/B4A/B4I apps and less-so with NON-GUI B4J apps.

After doing a few of those types of solutions, I went to, when creating a new B4J websocket server, doing a simple website interface to it. Even if the final solution had nothing to do with web connectivity. It's a whole lot easier/faster to refresh the display of a website after any modifications to the NON-GUI websocket Server than it is to rebuild the B4A/B4J/B4I app.

Then, several years ago I grew a brain and decided to make a universal (at least to my own projects) websocket connector and (testing) website that would pick up the new server as being there and connect to it automatically. No having to build something custom each time to connect, test, and monitor the development process from the client end. This came as an off-shoot of the universal B4J server monitor project and has greatly increased the productivity.
Very nice
 
Upvote 0

Swissmade

Well-Known Member
Licensed User
Longtime User
Very nice

You don´t need StatrtMessageLoop
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private cl As WebSocketClient
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
    cl.Initialize("WS")
End Sub
Sub WS_BinaryMessage (Data() As Byte)
    Log($"WS_BinaryMessage(${Data.Length})"$) 
End Sub
Sub WS_Closed (Reason As String)
    Log($"WS_Closed(${Reason})"$)
End Sub
Sub WS_Connected
    Log($"WS_Connected"$)
End Sub
Sub WS_TextMessage(Message As String)
    Log($"WS_TextMessage(${Message})"$)
End Sub
'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub Button1_Click
    cl.Connect("ws://192.168.2.5:51042/ws")
    'xui.MsgboxAsync("Hello world!", "B4X")
End Sub

View attachment 125034
Is it possible to share this test project or is this all source?
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
You don´t need StatrtMessageLoop
I have to think about that kind of solution a bit - since the server will need to be able to create a new websocket (and class to handle it) for each new connection. I suppose it can work, although perhaps it is more complicated.

But this is not the time: my eyes are closing in spite of my will 😄 :confused:

Thank you.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Is it possible to share this test project or is this all source?
I started a fresh new project and only added a wsclient to it.

The server part is the b4J Server app provided in the Tutorial.
 

Attachments

  • wsclient.zip
    8.5 KB · Views: 54
  • wsWebSocket.Server.zip
    1.9 KB · Views: 65
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I started a fresh new project and only added a wsclient to it.

The server part is the b4J Server app provided in the Tutorial.
I opened my eyes again a few minutes ago only :confused: and looked at the two projects just for two minutes, but...
should they demonstrate how to do without StartMessageLoop in the server project? I will look better but it seems to me that the project you have attached this command is there and the client project does not send data to the server. 🤔

Yes, I read that right:
The server part is the b4J Server app provided in the Tutorial.
but it is precisely the server project that would be useful to develop without StartMessageLoop.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
It seems to me that it is not possible to do without StartMessageLoop.

How should the server listen for new connections to which to associate a new websocket it should create?
 
Upvote 0
Top