Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. namespace SignalRSelfHost
  2. {
  3. class Program
  4. {
  5. static void Main(string[] args)
  6. {
  7. // This will *ONLY* bind to localhost, if you want to bind to all addresses
  8. // use http://*:8080 to bind to all addresses.
  9. // See http://msdn.microsoft.com/en-us/library/system.net.httplistener.aspx
  10. // for more information.
  11. string url = "http://localhost:8080";
  12. using (WebApp.Start(url))
  13. {
  14. Console.WriteLine("Server running on {0}", url);
  15. Console.ReadLine();
  16. }
  17. }
  18. }
  19. class Startup
  20. {
  21. public void Configuration(IAppBuilder app)
  22. {
  23. app.UseCors(CorsOptions.AllowAll);
  24. app.MapSignalR();
  25. }
  26. }
  27. public class MessageHub : Hub
  28. {
  29. public static event Action<string, string> MessageReceived = delegate { };
  30.  
  31. public void SendMessage(string name, string message)
  32. {
  33. MessageReceived(name, message);
  34. }
  35.  
  36. }
  37.  
  38. public class CustomType
  39. {
  40. public string Name;
  41. public int Id;
  42. }
  43. }
  44.  
  45. Handler handler;
  46. TextView statusField;
  47.  
  48. @Override
  49. protected void onCreate(Bundle savedInstanceState) {
  50. super.onCreate(savedInstanceState);
  51. setContentView(R.layout.activity_main2);
  52.  
  53. handler = new Handler();
  54. statusField = (TextView) findViewById(R.id.statusField);
  55.  
  56. Platform.loadPlatformComponent(new AndroidPlatformComponent());
  57. // Change to the IP address and matching port of your SignalR server.
  58. String host = "http://127.0.0.1:8080/";
  59. HubConnection connection = new HubConnection( host );
  60. HubProxy hub = connection.createHubProxy( "MessageHub" );
  61. ClientTransport transport = new ServerSentEventsTransport(connection.getLogger());
  62.  
  63. SignalRFuture<Void> awaitConnection = connection.start(transport);
  64. try {
  65. awaitConnection.get();
  66. }
  67. catch (InterruptedException e) {
  68. Log.d("CHECK", e.toString());
  69. e.printStackTrace();
  70. } catch (ExecutionException e) {
  71. Log.d("CHECK", e.toString());
  72. e.printStackTrace();
  73. }
  74.  
  75. hub.subscribe(this);
  76.  
  77. try {
  78. hub.invoke( "SendMessage", "Client", "Hello world!" ).get();
  79. hub.invoke( "SendCustomType",
  80. new CustomType() {{ Name = "Universe"; Id = 42; }} ).get();
  81. } catch (InterruptedException e) {
  82. // Handle ...
  83. } catch (ExecutionException e) {
  84. // Handle ...
  85. }
  86.  
  87. }
  88.  
  89. public void UpdateStatus(String status) {
  90. final String fStatus = status;
  91. handler.post(new Runnable() {
  92. @Override
  93. public void run() {
  94. statusField.setText(fStatus);
  95. }
  96. });
  97. }
  98.  
  99. public class CustomType
  100. {
  101. public String Name;
  102. public int Id;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement