B4J Question is there any sample on how a B4j non-ui app can comunicate with an B4j UI program?

omarruben

Active Member
Licensed User
Longtime User
I need to use a non-ui webserver with an UI app, any sample how to communicate between both apps?
 

aeric

Expert
Licensed User
Longtime User
If I understand correctly, you want example of how a client app (UI) send and/or receive data to/from a server app (non-UI).
I have Web API server and client templates and also JRDC2 server and client templates.
 
Upvote 0

omarruben

Active Member
Licensed User
Longtime User
If I understand correctly, you want example of how a client app (UI) send and/or receive data to/from a server app (non-UI).
I have Web API server and client templates and also JRDC2 server and client templates.
yes, a made an mp3 player in B4J for desktop, and I want to create a remote control using a web browser to play,stop, o play next song, really simple to achieve if the B4J app can have a http server like Android app
 
Upvote 0

omarruben

Active Member
Licensed User
Longtime User
Why not upload the mp3 to a server and share the link?
it is just a easy way to let old people to listen music, without tech , just open the browser and click on play button or click next. Many people i know is feed up with complicated apps...etc.. winamp use to be simple buy can not be installed on new systems... so I decided to make my own
 
Upvote 0

omarruben

Active Member
Licensed User
Longtime User
well, I did it, but still can't send a page to the browser (i need help )

B4X:
Sub AStream_NewData (Buffer() As Byte)
    Dim txt As String = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
    Dim components() As String
    components = Regex.Split("\r\n",txt) 
    If components(0) = "GET /test HTTP/1.1" Then
        Log("test")
    End If
  
    If components(0) = "GET /play HTTP/1.1" Then
        Log("play")
    End If
  
    If components(0) = "GET /next HTTP/1.1" Then
        Log("next")
    End If
  
    If components(0) = "GET /stop HTTP/1.1" Then
        Log("stop")
    End If
  
    If components(0) = "GET /autodj HTTP/1.1" Then
        Log("autodj")
        Button8_Click
    End If
      
    Dim  dd As String =$"HTTP/1.1 200 OK
    Content-Type: text/html; charset=utf-8
    Content-Length: 48
    Connection: Closed 
    <html><body><h1>Hello, World!</h1></body></html>"$
  
       astream.Write(dd.GetBytes("UTF8")) ' <----- it does not show nothing on the browser
    astream.SendAllAndClose
  
  
End Sub
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
well, I did it, but still can't send a page to the browser (i need help )

B4X:
Sub AStream_NewData (Buffer() As Byte)
    Dim txt As String = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
    Dim components() As String
    components = Regex.Split("\r\n",txt)
    If components(0) = "GET /test HTTP/1.1" Then
        Log("test")
    End If
 
    If components(0) = "GET /play HTTP/1.1" Then
        Log("play")
    End If
 
    If components(0) = "GET /next HTTP/1.1" Then
        Log("next")
    End If
 
    If components(0) = "GET /stop HTTP/1.1" Then
        Log("stop")
    End If
 
    If components(0) = "GET /autodj HTTP/1.1" Then
        Log("autodj")
        Button8_Click
    End If
     
    Dim  dd As String =$"HTTP/1.1 200 OK
    Content-Type: text/html; charset=utf-8
    Content-Length: 48
    Connection: Closed
    <html><body><h1>Hello, World!</h1></body></html>"$
 
       astream.Write(dd.GetBytes("UTF8")) ' <----- it does not show nothing on the browser
    astream.SendAllAndClose
 
 
End Sub
I don't know why you make it so complicated.
I think you may just need to add <audio> html tag to load the source mp3 file.
https://www.w3schools.com/html/html5_audio.asp
 
Upvote 0

MicroDrie

Well-Known Member
Licensed User
Longtime User
The point is that a web browser reads an HTML page into the DOM and is done. So you have to update the contents of the DOM when something changes, such as the next issue. This can be done in two ways: First, by reloading the entire page or, second, by replacing part of the page in the DOM with the updated content. You can use this approach, for example, in jSoup.

Personally, I think a simple solution is preferable to all kinds of technical feats. Because if the goal is to give people a simple listening experience, why not have a selection list created on the server and send that selection list to the intended listener. Maybe you can use JACo MP3 Player is a cross platform java mp3 player. Although this library is old (2010), it has:
  • very low CPU usage (~2%)
  • incredible small library (~90KB)
  • doesn't need JMF (and his system dependent plug-ins)
  • easy to integrate in any application (for the simplest case you need to write only one line of code)
  • easy to integrate in any web page (as a java applet)
  • well-documented interface description
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
Hi there...

let me see if i get it...

You have an MP3 Player playing Mp3s and you wanna control it (Play, Stop, Pause, Next, click on specific song) from web browser running at any device of a network... Yes sound like winamp... :) (Winamp runs in new systems... and there are also many apps in google play to control it)

So in MP3 Player side - you must have also running a jserver (b4j non-ui) that will wait (controls-from clients/web browser devices) and in the same time will control the mp3 player... and also get the state of app,list of songs... what is doing...

That will be easy for a lan... but if you want to control it outside the lan... use MQTT... with the same logic...
 
Upvote 0

omarruben

Active Member
Licensed User
Longtime User
Why we do programming? almost anything and everything is already done on the world, even Chatgpt is giving us ready to use code.....

Challenge!!!! we are special people that love to face a challenge !!!!

we need to do it our way....
we need to achieve the goal.....

thank you everybody for your suggestions and time!!!!
 
Upvote 0
Top