B4J Tutorial Consuming http streams

Discussion in 'B4J Tutorials' started by Erel, Nov 20, 2019.

  1. Erel

    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.

    [​IMG]
     

    Attached Files:

    Last edited: Nov 22, 2019
  2. Erel

    Erel Administrator Staff Member Licensed User

    Added an option to disconnect the connection.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice