Unable to send more than two input values using a

2019-09-05 15:53发布

问题:

I tried using the same while loop to achieve the same but its becoming a infinite loop

Sub Main()
    Dim connection = New HubConnection("http://localhost:8080")

    Dim myHub = connection.CreateHubProxy("myHub")

    connection.Start().Wait()
    Console.ForegroundColor = ConsoleColor.Yellow
    myHub.Invoke(Of String)("Chatter", Console.ReadLine) _
    .ContinueWith(
        Sub(task)
            If task.IsFaulted Then
                Console.WriteLine("Could not Invoke the server method Chatter: {0}", _
                                  task.Exception.GetBaseException())
            Else
                Console.WriteLine("Success calling chatter method")

            End If
        End Sub)

    myHub.On(Of String)("addMessage", _
        Sub(param)
            Console.WriteLine("Client receiving value from server: {0}", param.ToString())
        End Sub)
    Console.ReadLine()
End Sub

Could you point to me a way to keep sending the input values to the server ?

**Changes made **

Dim param As String = Console.ReadLine()
    While param <> "Exit"
        Console.WriteLine("Server Sending Value to Client X: {0}", param)
        myHub.On(Of String)("addMessage", _
            'Sub(param)
        Console.WriteLine("Client receiving value from server: {0}", param.ToString())
        'End Sub)
        Console.WriteLine("Enter a value to send to the Client.  Enter 'Exit' to quit")
        param = Console.ReadLine()
    End While
    Console.ReadLine()

but in this line : myHub.On(Of String)("addMessage", _ ITS THROWING ERROR "EXPRESSION EXPECTED"

回答1:

I suggest you create a message sending method, like:

Public Sub SendMessage(ByVal message as String)
'your code here
End Sub

then invoke this method in a while true loop where you check if the user wants to exit

While True
 Dim input = Console.ReadLine()
 If input <> "break"
   SendMessage(input)
 End If
End While

But if this is using SignalR, i suggest you check this guide. (It's in C# though)