如何在VB控制台应用程序中获取用户输入值?

时间:2016-10-05 12:41:41

标签: vb.net

如何在以下代码中获取用户输入值:

Sub Main()
    Dim url As String = "http://localhost:8080/"
    Using WebApp.Start(Of Startup)(url)
        Console.ForegroundColor = ConsoleColor.Green
        Console.WriteLine("Server running on {0}", url)
        Console.WriteLine("Press any key to start sending events to connected clients")
        Console.ReadLine()
        Dim context As IHubContext = GlobalHost.ConnectionManager.GetHubContext(Of MyHub)()
        For x As Integer = 0 To 5
            System.Threading.Thread.Sleep(3000)

            Console.WriteLine("Server Sending Value to Client X: " + x.ToString())
            context.Clients.All.addMessage(x.ToString())
        Next
        Console.ReadLine()
    End Using
End Sub
Public Class Startup
    Public Sub Configuration(ByVal app As IAppBuilder)
        Dim config = New HubConfiguration With {.EnableCrossDomain = True}
        app.MapHubs(config)
    End Sub
End Class
<HubName("myHub")> _
Public Class MyHub
    Inherits Hub
    Public Sub Chatter(param As String)
        Console.WriteLine(param)
        Clients.All.addMessage(param)
    End Sub

客户代码

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

当我尝试使用While循环时,我无法在收到两个输入值后一次退出。

我所做的更改 myHub.On(Of String)("addMessage", _

 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)
    Dim input As String = Console.ReadLine()
    While input <> "Exit"
        Console.WriteLine("Server Sending Value to Client X: {0}", input)
        myHub.On(Of String)("addMessage", _
          Console.WriteLine("Enter a value to send to the Client.  Enter 'Exit' to quit")
        input = Console.ReadLine()
    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

End Sub

1 个答案:

答案 0 :(得分:0)

如果您需要向服务器发送多个输入,请尝试使用循环。像这样(未经测试):

Sub Main()
    Dim url As String = "http://localhost:8080/"
    Using WebApp.Start(Of Startup)(url)
        Console.ForegroundColor = ConsoleColor.Green
        Console.WriteLine("Server running on {0}", url)
        Console.WriteLine("Press any key to start sending events to connected clients")
        Console.ReadLine()
        Dim context As IHubContext = GlobalHost.ConnectionManager.GetHubContext(Of MyHub)()

        Console.WriteLine("Enter a value to send to the server.  Enter 'Exit' to quit")

        Dim input As String = Console.ReadLine()
        While input <> "Exit"
            Console.WriteLine("Server Sending Value to Client X: {0}", input)
            context.Clients.All.addMessage(input)

            Console.WriteLine("Enter a value to send to the server.  Enter 'Exit' to quit")
            input = Console.ReadLine()
        End While
    End Using
End Sub