B4J Question WebSocket client with Prefix mode?

Arthur Ávila

Member
Licensed User
Following the tutorials of AsyncStreams I have a B4J Server running. I need to implement a websocket client which will send and receive some forms information. The client application right now is implemented mostly with JS/JQuery/PHP. Also there is an B4A app which will be sync with the web client. So... The problem is the web app.

My question is about the Prefix mode and the protocol that should be used. I'm not sure how to implement in a way that both sides can communicate.

I'm considering to use this package https://github.com/jbloemendal/jquery-simple-websocket
I can provide a protocol, for example
B4X:
var socket = $.simpleWebSocket(
    {
        url: 'ws://127.0.0.1:3000/',
        protocols: 'your_protocol', // optional

Right now I can't figure how to proceed.. I could use any other package or pure JS etc.

Any ideas on the implementation of the "prefix" protocol (client) would be great! Thanks!
 

Arthur Ávila

Member
Licensed User
True, I guess my question was messy..

But specifically, there is an implementation of AsyncStreams Prefix mode in JS/jQuery? Something like the C# example..

I've been tryin', so far what I've..

B4X:
Dim dataForm As dataEnviar
            dataForm.Initialize
            dataForm.action = "login_user"
            Private mDados As Map : mDados.Initialize
            mDados.Put("loginUser",edLoginLogin.Text)
            mDados.Put("passwordUser",edPasswordLogin.Text)
            dataForm.dados = mDados
            CallSub2(Starter, "SendData", ser.ConvertObjectToBytes(dataForm))

Then it looks like

B4X:
* Service (starter) Create *
* Service (starter) Start *
Conectado: false
* Activity (main) Create, isFirst = true *
* Activity (main) Resume *
Conectado: true
[Image1=[B@664d90f, Image2=[B@3ad2d9c, IsInitialized=true
, action=login_user, dados=(MyMap) {loginUser=popo, passwordUser=popo}]

And web...

B4X:
ava.lang.RuntimeException: Message size too large. Prefix mode can only work if both sides of the connection follow the 'prefix' protocol.
        at anywheresoftware.b4a.randomaccessfile.AsyncStreams$AIN.run(AsyncStreams.java:220)
        at java.lang.Thread.run(Thread.java:748)
java.lang.RuntimeException: Message size too large. Prefix mode can only work if both sides of the connection follow the 'prefix' protocol.
        at anywheresoftware.b4a.randomaccessfile.AsyncStreams$AIN.run(AsyncStreams.java:220)

On resume...

I will have an application that opens a socket and listens on it.

I need to communicate via this socket to that application using a javascript running on a browser. So I need to send some data on this socket so that the app which is listening on this socket can take that data, do some stuff and get some more data and put it back on the socket that my javascript needs to read and print it in the browser..


I'm not sure how to continue.

Thanks for the help!
 
Last edited:
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Why does the browser application have to communicate via sockets to the web server? Why not POST/GET/standard HTTP call method? The server (B4J?) can be set up to handle both WebSocket connections and standard HTTP calls. The standard server handler can be a front end to the same functionality that the web socket handler offers.

Now if your B4J server is hosting the HTML that is used by a browser to interface with you B4J server, than you need to take a close look at the examples provided here:
https://www.b4x.com/android/forum/threads/webapp-web-apps-overview.39811/#content

An example on how to communicate via WebSockets with your B4A application with a B4J WebSocket server: https://www.b4x.com/android/forum/threads/websocket-client-library.40221/
 
Upvote 0

Arthur Ávila

Member
Licensed User
Yeah I could migrate, but all queries are ready, considering all data etc.

I'm trying to figure how to continue and use JS to connect

B4X:
Private Sub ListenForConnections
    Do While working
        server.Listen
        Wait For Server_NewConnection (Successful As Boolean, NewSocket As Socket)
        If Successful Then
            
            Try
                Private client As Socket
                Private astream As AsyncStreams
                client = NewSocket
                astream.InitializePrefix(client.InputStream, False, client.OutputStream, "astream")
                Private arr(2) As Object
                arr(0) = client
                arr(1) = astream
                lConexoes.Add(arr)
            Catch
                Log("")
            End Try
        
            
        End If
    Loop
End Sub

Sub AStream_Error
    
End Sub

Sub AStream_Terminated

End Sub

Sub AStream_NewData (Buffer() As Byte)
    Dim dl As dataEnviar = ser.ConvertBytesToObject(Buffer)
    Private astream As AsyncStreams = Sender
...
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
The client application right now is implemented mostly with JS/JQuery/PHP
This is so confusing. You have something working. What you have is really not clear. So you have a PHP backend (server) that you are communicating with in JS/JQuery? If this is already using WebSockets, why are you looking into using a WebSocket client JavaScript code? If it is to connect to JServer via WebSocket, you are doing it wrong. The JavaScript file you need is included in the sample applications that I pointed you to. The file is called b4j_ws.js. But then again, the code you show in post #5 is not even associated with WebSockets. So what are you trying to reproduce in B4J that you currently have working?
 
Upvote 0

Arthur Ávila

Member
Licensed User
You are confusing different things. The "correct" way to build such a server is with jServer and use WebSocketHandler class on the client.

AsyncStreams is not used for this.

Ok, I had some chat with OliverA, I'll try to be clearer.

I've a VPS which is running this B4J example https://www.b4x.com/android/forum/threads/b4x-network-asyncstreams-b4xserializator.72149/
I've used both B4A and B4J examples, now I need to know how to connect the web app. So I've a XAMPP application which I need to adapt.

Both B4A and B4J can communicate with the VPS, now I need PHP or JS to communicate with the VPS.
Since prefix mode is not implemented in both JS and PHP, the solution is to implement it, yes? Or create my own protocol.

I've saw the .NET and VB6 examples, but I can't figure how to proceed.
 
Upvote 0
Top