Advertisement
Guest User

Untitled

a guest
Oct 20th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. Module Module1
  2. Dim _hubConnection As HubConnection
  3. Dim _myHubProxy As IHubProxy
  4.  
  5. Async Function InitiateConnection() As Task
  6. _hubConnection = New HubConnection("http://localhost:4848")
  7.  
  8. AddHandler _hubConnection.Closed,
  9. Sub()
  10. Console.BackgroundColor = ConsoleColor.White
  11. Console.ForegroundColor = ConsoleColor.Red
  12. Console.WriteLine("CONNECTION STATE: CLOSED")
  13. Console.ResetColor()
  14. End Sub
  15.  
  16. AddHandler _hubConnection.[Error], Sub(e)
  17. Console.BackgroundColor = ConsoleColor.Red
  18. Console.ForegroundColor = ConsoleColor.White
  19. Console.WriteLine("Hub connection error: " & e.ToString())
  20. Console.ResetColor()
  21. End Sub
  22. AddHandler _hubConnection.StateChanged, Sub(state)
  23. Console.BackgroundColor = ConsoleColor.DarkYellow
  24. Console.ForegroundColor = ConsoleColor.Black
  25. Console.WriteLine("Connection State Changed From {0} To {1}", state.OldState, state.NewState)
  26. Console.ResetColor()
  27. If state.NewState = ConnectionState.Disconnected Then
  28. Console.BackgroundColor = ConsoleColor.Blue
  29. Console.ForegroundColor = ConsoleColor.White
  30. Console.WriteLine("Reconnecting in 500 ms")
  31. Console.ResetColor()
  32. Thread.Sleep(500)
  33. _hubConnection.Start() ' Will never reconnect. Workaround: Recreate connection Task.Run(Sub() hubConnection.Start().Wait()) _hubConnection.Start(); ' Will never reconnect. Workaround: Recreate connection CreateConnection().Start();
  34.  
  35. ElseIf state.NewState = ConnectionState.Connected Then
  36. Console.BackgroundColor = ConsoleColor.Green
  37. Console.ForegroundColor = ConsoleColor.Black
  38. Console.WriteLine("CONNECTION STATE: CONNECTED")
  39. Console.ResetColor()
  40. End If
  41. End Sub
  42.  
  43. _myHubProxy = _hubConnection.CreateHubProxy("myHub")
  44. Await _hubConnection.Start()
  45.  
  46. _myHubProxy.[On](Of String)("heartbeat", Sub() Console.Write("Recieved heartbeat " & vbLf))
  47.  
  48. End Function
  49. Sub Main()
  50. Try
  51. InitiateConnection().Wait()
  52.  
  53. Catch ex As Exception
  54. Console.WriteLine( ex.ToString())
  55. End Try
  56. End Sub
  57. End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement