B4J Question SendToSelect item command

klingon467

Member
Licensed User
Longtime User
Hi,
i have client and server connection i using serverSocket and AsyncStream.... now my client are add into tableview and ok! but i want send a command to a specific client....

my approach:
B4X:
Sub manna(message As String)'routine send data/streaming
  Try
   For Each row() As Object In tbl1.SelectedRow
     Dim mexB() As Byte
     mexB = message.GetBytes("UTF8")
     astream.Write2(mexB, 0, mexB.Length)
next
   Catch
  Msgbox.Show(LastException.message, "Error!")
  End Try
      
End Sub

how can I do?
thanks
 

klingon467

Member
Licensed User
Longtime User
Not sure that I understand. Are there multiple clients connected?
Yes, i have more clients....
I want to send to a specific client a string....
I'm studying a sample created in vb.net and I would like to replicate it on B4J (server side) and B4A (client side)

vb.net example:
B4X:
    Sub SendToSelected(ByVal Message As String)
        For Each item As ListViewItem In lstClients.SelectedItems
            Try
                Dim c As connection = DirectCast(item.Tag, connection)
                c.Send(Message) 'send streaming data
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
        Next
    End Sub

thanks @Erel
 
Upvote 0

klingon467

Member
Licensed User
Longtime User
The VB.Net code you posted maintains a list of connections not strings. You need to maintain a similar list of AsyncStreams.

See this example: https://www.b4x.com/android/forum/threads/b4j-cctv-example.34695/#post-203347 (in the second post).
thanks @Erel good share.....
I'm trying to adapt the example of post....
I should do the reverse thing I should send a string to a selected item on the Tableview
as I understand it the main screen element that I should use it
Dim connections as Map

connections.put
connections.Get

for example:
B4X:
Sub chChat_CheckedChange(Checked As Boolean)
  
    If chChat.Checked = True Then
    Dim li As TableView
    li.Initialize("li")
    connections.Put(astream, li)
    manna("enChat")
    Else If chChat.Checked = False Then
    manna("disChat")
    Else
        Msgbox.Show(LastException.Message, "RAT4A")
    End If
  
End Sub

function:
B4X:
Sub manna(message As String)'routine per invio dati/streaming
    Try
        Dim mexB() As Byte
        mexB = message.GetBytes("UTF8")
        astream.Write2(mexB, 0, mexB.Length)
    Catch
        Msgbox.Show(LastException.message, "RAT4A")
    End Try
    End Sub

right?
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I don't think that this code is correct. Connections map should hold the currently active connections. The value can be empty or can be any value you like to tie to the connection.

In your case you should create another map that maps between your client ids and the AsyncStream connections. This will allow you to send messages to specific clients.

Note that a for a much more scalable solution you can use web sockets: Custom WebSocket Based Push Framework
 
Upvote 0

klingon467

Member
Licensed User
Longtime User
Hi @Erel
I developed a small example client-server (B4J - B4A) but I would not know how to implement the data transmission to a specific client as I did I op can send data only at the last connected client ...
you take a look?
thank you
 

Attachments

  • B4AClient.zip
    450.8 KB · Views: 194
  • B4JServer.zip
    28.9 KB · Views: 180
Upvote 0
Top