B4J Tutorial Consuming http streams

Erel

Administrator
Staff member
Licensed User
Standard http communication is made of a request being sent from the client to the server and the server response sent back to the client.
As I'm sure you know you should use OkHttpUtils2 for such requests.

There are other protocols such as WebSockets that allow bi-directional and long lasting communication between the client and server.

There are some cases where the server keeps the connection open and streams data to the client. Assuming that WebSockets are not used then such cases require non-standard handling.

The attached MJPEG class uses a custom OutputStream (implemented with inline Java) together with B4XBytesBuilder to read the data as it comes and keep the connection open.
It is a port of this decoder, which was implemented with raw sockets and AsyncStreams.
The Data_Available event is raised whenever new data is available.

Note that this code can be easily ported to B4A.

If making many connections at the same time then it is better to move OkHttpClient out of the class and reuse it.

 

Attachments

Last edited:

woniol

Active Member
Licensed User
Hi,

Can we have an exmaple code ported to B4A.
With the original custom OutputStream does not work with B4A.
 

Erel

Administrator
Staff member
Licensed User
Please post your B4A project and explain what "does not work" means.
 

woniol

Active Member
Licensed User
Changed:
B4X:
Private const PackageName As String = "b4j.example" '<------ change as needed
to
B4X:
Private const PackageName As String = "b4a.example" '<------ change as needed
and it works like a charm :)
 
Top