Android Question Send objects via web socket

Erel

B4X founder
Staff member
Licensed User
Longtime User
Note that it is easier to work with B4XSerializator (it was not available when the example above was written).

You can use these two subs to convert an object to string and vice versa:
B4X:
Sub Process_Globals
   Private su As StringUtils
   Private serializator As B4XSerializator
End Sub

Public Sub ConvertObjectToString(obj As Object) As String
    Return su.EncodeBase64(serializator.ConvertObjectToBytes(obj))
End Sub

Public Sub CovertStringToObject(str As String) As Object
   Return serializator.ConvertBytesToObject(su.DecodeBase64(str))
End Sub
 
Upvote 0

Roberto P.

Well-Known Member
Licensed User
Longtime User
thanks Erel, I try now.

There are limits on the size of the object?

Greetings
 
Upvote 0

Roberto P.

Well-Known Member
Licensed User
Longtime User
ok.
do you think is most effective and efficient way to send data

B4X:
Sub SendObject (Obj As Object, aTag As String)
 
   raf.WriteObject(Obj, True, 0)
   Dim size As Int = raf.CurrentPosition
    ' Log("Dimensione: " & size)
   Dim data(size) As Byte
   raf.CurrentPosition = 0
   
   Do While raf.CurrentPosition < size
     raf.ReadBytes(data, raf.CurrentPosition, size - raf.CurrentPosition, raf.CurrentPosition)
   Loop

   Dim j As HttpJob
   j.Initialize("send_data", Me)
 
   j.PostBytes(link & "/Sync", data)

  
End Sub

or web sockets and B4XSerializator methods?
thanks
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
As I wrote in my first post you should use B4XSerializator instead of the code above.
There are two steps:
1. Convert the object to bytes (or string). You should use B4XSerializator here.
2. Send the bytes (or string).

Which one is more effective depends on your requirements. If you want to maintain an active, bi-directional connection then you should use web sockets.
 
Upvote 0

Roberto P.

Well-Known Member
Licensed User
Longtime User
I had not realized that it is better to use serialization also send items via HttpJob. I advice to use this medoto for the procedures for exchanging data that I've done or just for the new?

I could not find a complete example! Can you tell me if there is some thread?

Thanks again
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The code you posted also serializes the object. It is just more cumbersome.

You can replace:
B4X:
raf.WriteObject(Obj, True, 0)
Dim size As Int = raf.CurrentPosition' 
Log("Dimensione: " & size
)Dim data(size) As Byte
 raf.CurrentPosition = 0
 Do While raf.CurrentPosition < size
 raf.ReadBytes(data, raf.CurrentPosition, size - raf.CurrentPosition, raf.CurrentPosition)
Loop
With:
B4X:
Dim data() As Byte = serializator.ConvertObjectToBytes(Obj)
 
Upvote 0

Roberto P.

Well-Known Member
Licensed User
Longtime User
goods..

side B4J \ Server I read again with this method or should I change?


B4X:
'helper sub to read objects from the input stream
Public Sub ReadObject (In As InputStream) As Object
    Dim out As OutputStream
    out.InitializeToBytesArray(0)
    File.Copy2(In, out)
    Dim raf2 As RandomAccessFile
    raf2.Initialize3(out.ToBytesArray, False)
    Dim res As Object = raf2.ReadObject(0)
    raf2.Close
    Return res
End Sub

Thanks again
 
Upvote 0

Roberto P.

Well-Known Member
Licensed User
Longtime User
Hi Erel,
This is the function to read the data on the server side B4J. not by errors.
You can politely say if it is correct or can you improve?


B4X:
public Sub ReadObjectSerialized(Input As InputStream) As Object
 
    Dim serializator As B4XSerializator
 
    Try
     
        Dim out As OutputStream
        out.InitializeToBytesArray(0)
        File.Copy2(Input, out)
     
        Return  serializator.ConvertBytesToObject(out.ToBytesArray)
     
    Catch
        Log(LastException)
    End Try
 
 
End Sub

Thank you
 
Upvote 0
Top