Android Question how create websocket server to B4A

Roberto P.

Well-Known Member
Licensed User
Longtime User
Hi all
You can give me the best way to create a websocket in an android app, which acts as a server, to another android application and b4j.

thank in advance
 

DonManfred

Expert
Licensed User
Longtime User
Isn´t wecksocket server a b4j library?

As far as i know you can not use Android to be a Websocketserver.
 
Upvote 0

Roberto P.

Well-Known Member
Licensed User
Longtime User
yes it is a B4J library. Yes, you can use B4A as a server (see httpserverexample), but I need websockets to keep the connection active and to be able to exchange data between two apps.
Isn´t wecksocket server a b4j library?

As far as i know you can not use Android to be a Websocketserver.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Upvote 0

Roberto P.

Well-Known Member
Licensed User
Longtime User
There is no server side implementation of WebSockets for B4A.

I recommend you to use MQTT instead.


ok. Can I use MQTT to for send image as CCTV camera?
thank you
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
What about informing about mqtt and its payload?

I don´t know mqtt much. Don´t know the limit of one message.
But if it is only text then you can create a base64 of an image and send this base64 string.

mqtt expects a bytearray as payload. So it should be possible to send images i guess. I would just use the available methods.
look at the chatexample project.

B4X:
Public Sub SendMessage(Body As String)
    If connected Then
        client.Publish2("all", CreateMessage(Body), 0, False)
    End If
End Sub

Public Sub Disconnect
    BroadcastTimer.Enabled = False
    DiscoveredServer = ""
    CallSub(Main, "UpdateState")
    If connected Then
        client.Publish2("all/disconnect", serializator.ConvertObjectToBytes(Name), 0, False)
        client.Close
    End If
End Sub

Private Sub CreateMessage(Body As String) As Byte()
    Dim m As Message
    m.Initialize
    m.Body = Body
    m.From = Name
    Return serializator.ConvertObjectToBytes(m)
End Sub

As i never used mqtt in production and i just checked one of the available example codes i may be wrong.
But i´m positive that you can create your own Message customType which holds an Image (the bytes, not base64) in one of its properties and use Serializator to convert the customtype into a payload for mqtt. So my answer is YES.
 
Upvote 0

Roberto P.

Well-Known Member
Licensed User
Longtime User
i'm trying the symple chat, but have this error:

*** Service (starter) Create ***
starter_service_create (java line: 457)
java.net.BindException: bind failed: EADDRINUSE (Address already in use)
at libcore.io.IoBridge.bind(IoBridge.java:104)
at java.net.PlainDatagramSocketImpl.bind0(PlainDatagramSocketImpl.java:103)
at java.net.AbstractPlainDatagramSocketImpl.bind(AbstractPlainDatagramSocketImpl.java:92)
at java.net.DatagramSocket.bind(DatagramSocket.java:411)
at java.net.DatagramSocket.<init>(DatagramSocket.java:261)
at java.net.DatagramSocket.<init>(DatagramSocket.java:318)
at java.net.DatagramSocket.<init>(DatagramSocket.java:290)
at anywheresoftware.b4a.objects.SocketWrapper$UDPSocket.Initialize(SocketWrapper.java:444)
at mqtt.chat.starter._service_create(starter.java:457)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:196)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:176)
at mqtt.chat.starter.onCreate(starter.java:56)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3558)
at android.app.ActivityThread.-wrap4(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1812)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:7000)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
Caused by: android.system.ErrnoException: bind failed: EADDRINUSE (Address already in use)
at libcore.io.Linux.bind(Native Method)
at libcore.io.ForwardingOs.bind(ForwardingOs.java:60)
at libcore.io.IoBridge.bind(IoBridge.java:100)
... 21 more
 
Upvote 0
Top