Android Tutorial Custom WebSocket Based Push Framework

vfafou

Well-Known Member
Licensed User
Longtime User
Hello!
Is there any way to search and find memory leaks?
How can I force release - dispose - destroy of a thread?
Is it possible runtime errors to leave bad data in memory?
Could Try-Catch-End Try block help to avoid such situations?
Thank you in advance!
 
Last edited:

Reids

Member
Licensed User
Longtime User
Hello how I can start websocket connection when activity is closed = true (apps is killed)
I can call the service when app sleep to call connection sub

B4X:
If IsPaused(Main) = True Then
        Log("apps killed")
        Connect
    End If


B4X:
Private Sub Connect
    Try
        Dim wsh As WebSocketHandler
        ws.Initialize("ws")
        ws.Connect(Main.serverLink)
    Catch
    End Try
End Sub


but websocket connection could not be made, no connection is made it keep throwing me to reconnect

B4X:
Sub ReconnectTimer_Tick
    Try

        If ws.Connected Then
            Log("websocket still connected")
        Else
            Log("websocket is dead so reconnect")
            Connect
        End If
        'ReconnectTimer.Enabled = False

    Catch
    End Try
End Sub
 

iCAB

Well-Known Member
Licensed User
Longtime User
Hi Erel
Where do I find the latest:
1. sample code client (B4A)
2. server code
3. latest client library

Thanks
 

vfafou

Well-Known Member
Licensed User
Longtime User
Hello!
I want to inform you that after my latest configurations to my Database Server, the framework rocks!
I have ~650 concurrent tablets connected to the web server and the response is rapid!
Thanks Erel for all his help!

I think that it's time to secure the web server.
I mean that I want to add an authentication process.
Which is the best practice for securing a web sockets server?

Thank you in advance!
 

iCAB

Well-Known Member
Licensed User
Longtime User
Hi Erel
I downloaded the following Sample projects:
1. WebSocketExample (B4A) & Server B4A-WebSocket (B4J)... Things are working no problem

For example: Say I have 2 clients:
1. Client "A"
2. Client 'B"
when the clients sends a message, it adds an identifier in the message (Client_X ).
Say the main program is reading a log of messages: each message is addressed to a specific Client. For example:
Client_A: Send A
Client_B: Sent B

Can you please show me the sample code that can be used by the main program to send the message to the proper client

Thank you
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Which is the best practice for securing a web sockets server?
WebSocketClient currently doesn't support wss connections.

Can you please show me the sample code that can be used by the main program to send the message to the proper client
You will need to go over the code and learn it. NewMessage from PushShared expects two parameters. The message text and an array of client ids. You need to call this method with CallSubDelayed with the message and specific client id.
 

iCAB

Well-Known Member
Licensed User
Longtime User
Hi Erel

May be we are not talking about the same code. Let me clarify something please.
In B4A-WebSocket Example, I see this code in the timer
B4X:
Sub Timer1_Tick
    ws.RunFunction("ServerTime", Array As Object(DateTime.Time(DateTime.Now)))
    ws.Flush
End Sub

The code above references the instance for the socket to send data.
I was wondering how can I reference or identify the instance referred to in this code from the main program.

Thanks again
 

vfafou

Well-Known Member
Licensed User
Longtime User
Hi iCAB,
See the following modules (hope this helps!):

From PushB4A
B4X:
Public Sub SendMessages(Messages As List)
    ws.RunFunction("NewMessage", Messages)
    ws.Flush
End Sub

From PushShared
B4X:
Private Sub SendUserMessages(id As String, pb As PushB4A)
    PSQL = Main.Pool.GetConnection
   
    Dim table As List = DBUtils.ExecuteMemoryTable(PSQL, _
        "SELECT messages_d.id_msg, message FROM messages_q, messages_d WHERE messages_q.id_msg = messages_d.id_msg AND id_device = ?", _
            Array As String(id), 0)
   
    PSQL.Close
   
    If table.Size > 0 Then
   
        Dim msgs As List
       
        msgs.Initialize
       
        For Each row() As String In table
            msgs.Add(row(0))
            msgs.Add(row(1))
        Next
       
        CallSubDelayed2(pb, "SendMessages", msgs)
   
    End If
   
End Sub
B4X:
Private Sub SendSingleMessage(MsgId As String, Msg As String, pb As PushB4A)
    Dim msgs As List = Array (MsgId, Msg)

    CallSubDelayed2(pb, "SendMessages", msgs)

End Sub
 

vfafou

Well-Known Member
Licensed User
Longtime User
WebSocketClient currently doesn't support wss connections.
Is it possible to support wss in the near future?
What else I could do to secure my web server?

Thank you in advance!
 

vfafou

Well-Known Member
Licensed User
Longtime User
Hello Erel!
OK, I've got it!
But how to protect it from possible hacker attacks?
There is a port open and available to the web!
 

Roberto P.

Well-Known Member
Licensed User
Longtime User
Hello to all
I entered your framework in my app, sometimes when I close the app and relaunch is not called the main activity. This occurs because there is no connection to the server and made the call to connect. I tried to insert DoEvents, but does not solve the problem.
Can you tell me if there is another way to solve the problem.
thank you
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…