无法使用while循环发送两个以上的输入值

时间:2016-10-06 12:07:02

标签: vb.net signalr

我尝试使用相同的while循环来实现相同但它变成无限循环

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

你能指点我一种方法来继续将输入值发送到服务器吗?

**所做的更改**

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()

但在这一行:myHub.On(Of String)(“addMessage”,_ ITS THROWING ERROR“EXPRESSION EXPECTED”

1 个答案:

答案 0 :(得分:0)

我建议你创建一个消息发送方法,如:

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

然后在while循环中调用此方法,检查用户是否要退出

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

但如果使用SignalR,我建议您查看this指南。 (虽然它在C#中)