B4R Question Running 2 streams on one connection

barx

Well-Known Member
Licensed User
Longtime User
Hi

What would be the correct way (if possible at all) to run 2 stream objects on one connection?

Using ESP8266, so the connection is Wifi.

Wanting to have MQTT and be able to give HTTP responses (using AsyncStream)

With the code I have so far, which I thought was pretty much complete but this issue has popped up, either system works fine on their own (MQTT or HTTP), but when both are in use together I tend to get the MQTT payload tagged onto my HTTP JSON response and then MQTT fails to publish from there on. I've been messing for a few hours now and struggling to be honest.

A Bit of code samples:
MQTT is initialized with
B4X:
MQTT.Initialize2(server.Socket.Stream, "mqtt.thingspeak.com", 1883, "xxxxxxxxxxxxx", "MQTT_MessageArrived", "MQTT_Disconnected")
    MQTTOptions.Initialize("xxxxxxxxxxx", "xxxxxxxxxxxxxxxx")
'xxxxxxxxx = private data removed

AsyncStream initialized with:

B4X:
Sub server_NewConnection (NewSocket As WiFiSocket)
    Log("New Connection")
    astream.Initialize(NewSocket.Stream, "astream_NewData", "astream_Error")
End Sub

Once a HTTP rresponse is complete I finish off with this to flush it to the browser.

B4X:
        server.Socket.Stream.Flush
        server.Socket.close

As mentioned above, sometimes I get the MQTT data on the returned JSON http response and it looks like this


After this, HTTP seems to carry on working but MQTT fails. Any hints on how to resolve this conflict?
Just thinking now, Do I need to have 2 server objects, which is an instance of WifiServerSocket?
Do I need to just work on my timings and/or create a flagging system that sets a flag when an http is in progress so don't try to MQTT?
 

barx

Well-Known Member
Licensed User
Longtime User
Why are you using the same socket? Use a different socket for MQTT.

Because I have no idea what I'm doing
As I got to the end of the OP above it literally just popped into my head to create more than 1 WifiServerSocket. Is that what you mean ?

Cheers
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
Ok, Thanks

Will give it a blast and see how she goes.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…