B4J Question SendToSelect item command

Discussion in 'B4J Questions' started by klingon467, Jun 18, 2015.

  1. klingon467

    klingon467 Member Licensed 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:
    Code:
    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
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Not sure that I understand. Are there multiple clients connected?
     
  3. klingon467

    klingon467 Member Licensed User

    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:
    Code:
    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
     
  4. Erel

    Erel Administrator Staff Member Licensed User

  5. klingon467

    klingon467 Member Licensed User

    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:
    Code:
    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:
    Code:
    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: Jun 22, 2015
  6. Erel

    Erel Administrator Staff Member Licensed 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
     
  7. klingon467

    klingon467 Member Licensed 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
     

    Attached Files:

  8. Erel

    Erel Administrator Staff Member Licensed User

    The task is more complicated. Your code only deals with a single connection.

    As I see it you have two options:
    1. Learn the CCTV example that handles multiple connections.
    2. (simpler) Use the custom push framework.
     
Loading...