请使用以下代码更正我并澄清我的疑虑

时间:2016-10-07 13:34:28

标签: vb.net signalr.client

输入值发布两次,因为我在声明和in else语句后使用myHub.Invoke(Of String)("Chatter", Input)两个位置。

我不知道是否可以替换以下代码中的.continuewith块。

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

    Dim myHub = connection.CreateHubProxy("myHub")

    connection.Start().Wait()
    Console.ForegroundColor = ConsoleColor.Yellow
    Dim Input As String = Console.ReadLine
    myHub.Invoke(Of String)("Chatter", Input) _
    .ContinueWith(
        Sub(task)
            If task.IsFaulted Then
                Console.WriteLine("Could not Invoke the server method Chatter: {0}", _
                                  task.Exception.GetBaseException())
            Else
                myHub.Invoke(Of String)("Chatter", Input)
                Console.WriteLine("Enter a value to send to the Client.  Enter 'Exit' to quit")
                Input = Console.ReadLine()
                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

在替换continuewith block之后,客户端没有从服务器接收值。

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

Dim myHub = connection.CreateHubProxy("myHub")
connection.Start().Wait()
Console.ForegroundColor = ConsoleColor.Yellow
Dim Input As String = Console.ReadLine
myHub.Invoke(Of String)("Chatter", Input) _
.ContinueWith(
Sub(task)
If task.IsFaulted Then
Console.WriteLine("Could not Invoke the server method Chatter: {0}", _
task.Exception.GetBaseException())
Else
myHub.Invoke(Of String)("Chatter", Input)
Console.WriteLine("Enter a value to send to the Client. Enter 'Exit' to quit")
Input = Console.ReadLine()
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

1 个答案:

答案 0 :(得分:0)

(代表OP发布)

解决方案:

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

    Dim myHub = connection.CreateHubProxy("myHub")

    connection.Start().Wait()
    Console.ForegroundColor = ConsoleColor.Yellow
    myHub.On(Of String)("addMessage", _
     Sub(param)
          'If param.Length > 0 < 2 Then
          Console.WriteLine("Client receiving value from server: {0}", param.ToString())
          ' End If
      End Sub)
    Dim input As String = Console.ReadLine()
    myHub.Invoke(Of String)("chatter", input)
    While input <> "exit"
        input = Console.ReadLine()
        myHub.Invoke(Of String)("chatter", input)


    End While

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