'How to get user input values in VB console application?
How to get the user input values in below code:
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
CLIENT CODE
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
When I try to use the While loop I can't achieve the same where it exits once after it receives two input values.
Changes i made 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
Solution 1:[1]
If you need to send multiple inputs to the server, try using a loop. Something like this (untested):
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Chris Dunaway |
