PageRenderTime 56ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/signalr/signalr-tests.ts

https://gitlab.com/CatchLabs/DefinitelyTyped
TypeScript | 197 lines | 162 code | 23 blank | 12 comment | 7 complexity | d74a5c00ea1b90bc349953df0833338e MD5 | raw file
  1. /// <reference path="signalr.d.ts" />
  2. var connection = $.hubConnection();
  3. var contosoChatHubProxy = connection.createHubProxy('contosoChatHub');
  4. contosoChatHubProxy.on('addContosoChatMessageToPage', function (name, message) {
  5. console.log(name + ' ' + message);
  6. });
  7. connection.start().done(function () {
  8. // Wire up Send button to call NewContosoChatMessage on the server.
  9. $('#newContosoChatMessage').click(function () {
  10. contosoChatHubProxy.invoke('newContosoChatMessage', $('#displayname').val(), $('#message').val());
  11. $('#message').val('').focus();
  12. });
  13. }).fail(function () {
  14. console.log('Could not connect');
  15. });
  16. connection.qs = { 'version': '1.0' };
  17. $.connection.hub.url = '<yourbackendurl>';
  18. $.connection.hub.qs = { 'version': '1.0' };
  19. $.connection.hub.start({ transport: 'longPolling' });
  20. $.connection.hub.start({ transport: ['webSockets', 'longPolling'] });
  21. connection.start({ transport: 'longPolling' });
  22. connection.start({ transport: ['webSockets', 'longPolling'] });
  23. $.connection.hub.start().done(function () {
  24. console.log("Connected, transport = " + $.connection.hub.transport.name);
  25. });
  26. connection.hub.start().done(function () {
  27. console.log("Connected, transport = " + connection.transport.name);
  28. });
  29. $.connection.hub.connectionSlow(function () {
  30. console.log('We are currently experiencing difficulties with the connection.')
  31. });
  32. connection.connectionSlow(function () {
  33. console.log('We are currently experiencing difficulties with the connection.')
  34. });
  35. $.connection.hub.error(function (error) {
  36. console.log('SignalR error: ' + error)
  37. });
  38. connection.error(function (error) {
  39. console.log('SignalR error: ' + error)
  40. });
  41. connection.logging = true;
  42. $.connection.hub.logging = true;
  43. function test_client() {
  44. var connection = $.connection('/echo');
  45. connection.received(function (data) {
  46. console.log(data);
  47. });
  48. connection.error(function (error) {
  49. console.warn(error);
  50. });
  51. connection.stateChanged(function (change) {
  52. if (change.newState === $.signalR.connectionState.reconnecting) {
  53. console.log('Re-connecting');
  54. }
  55. else if (change.newState === $.signalR.connectionState.connected) {
  56. console.log('The server is online');
  57. }
  58. });
  59. connection.reconnected(function () {
  60. console.log('Reconnected');
  61. });
  62. connection.start();
  63. connection.start(function () {
  64. console.log("connection started!");
  65. });
  66. connection.stop();
  67. connection.start().done(function () {
  68. console.log("connection started!");
  69. });
  70. connection.start({ transport: 'longPolling' });
  71. connection.start({ transport: $.signalR.transports.webSockets });
  72. connection.start({ transport: ['longPolling', 'webSockets'] });
  73. connection.start({ waitForPageLoad: false });
  74. connection.start({ transport: 'longPolling' }, function () {
  75. console.log('connection started!');
  76. });
  77. connection.send("Hello World");
  78. var connection = $.connection('http://localhost:8081/echo');
  79. connection.start({ jsonp: true });
  80. }
  81. function test_connection() {
  82. var connection = $.connection('/echo');
  83. connection.received(function (data) {
  84. $('#messages').append('<li>' + data + '</li>');
  85. });
  86. connection.start();
  87. $("#broadcast").click(function () {
  88. connection.send($('#msg').val());
  89. });
  90. }
  91. interface MyHubConnection extends SignalR.Hub.Connection {
  92. someState: string;
  93. SomeFunction: Function;
  94. // My Hubs Client functions:
  95. client: {
  96. addMessage: (message: string) => void;
  97. };
  98. // My Hubs Server function:
  99. server: {
  100. send(message: string): any;
  101. };
  102. }
  103. interface SignalR {
  104. chat: MyHubConnection;
  105. myHub: MyHubConnection;
  106. }
  107. function test_hubs() {
  108. var chat = $.connection.chat;
  109. $.connection.hub.start()
  110. .done(function () { alert("Now connected!"); })
  111. .fail(function () { alert("Could not Connect!"); });
  112. $.connection.hub.logging = true;
  113. var myHub = $.connection.myHub;
  114. myHub.someState = "SomeValue";
  115. function connectionReady() {
  116. alert("Done calling first hub serverside-function");
  117. };
  118. myHub.SomeFunction = function () {
  119. alert("serverside called 'Clients.SomeClientFunction()'");
  120. };
  121. $.connection.hub.error(function () {
  122. alert("An error occured");
  123. });
  124. $.connection.hub.start()
  125. .done(function () {
  126. myHub.SomeFunction("whatever")
  127. .done(connectionReady);
  128. })
  129. .fail(function () {
  130. alert("Could not Connect!");
  131. });
  132. $.connection.hub.url = 'http://localhost:8081/signalr'
  133. $.connection.hub.start();
  134. var connection = $.hubConnection();
  135. var proxy = connection.createHubProxy('chat');
  136. var proxy = connection.createHubProxy('chat'),
  137. msg = 'hello',
  138. room = 'main';
  139. proxy.invoke('send', msg);
  140. proxy.invoke('send', msg, room);
  141. proxy.invoke('add', 1, 2)
  142. .done(function (result: any) {
  143. console.log('The result is ' + result);
  144. });
  145. proxy.on('addMessage', function (msg?) {
  146. console.log(msg);
  147. });
  148. //a listener may have more than 1 parameter, and you should be able to subscribe and unsubscribe
  149. function listenerWithMoreParams(id: number, anything: string) {
  150. console.log('listenerWithMoreParams -> ', arguments);
  151. };
  152. //subscribe
  153. proxy.on('listenerWithMoreParams', listenerWithMoreParams);
  154. var connection = $.hubConnection('http://localhost:8081/');
  155. connection.start({ jsonp: true });
  156. //unsubscribe
  157. proxy.off('listenerWithMoreParams', listenerWithMoreParams);
  158. }
  159. // Sample from : https://github.com/SignalR/SignalR/wiki/QuickStart-Hubs#javascript--html
  160. $(function () {
  161. // Proxy created on the fly
  162. var chat = $.connection.chat;
  163. // Declare a function on the chat hub so the server can invoke it
  164. chat.client.addMessage = function (message) {
  165. $('#messages').append('<li>' + message + '</li>');
  166. };
  167. // Start the connection
  168. $.connection.hub.start().done(function () {
  169. $("#broadcast").click(function () {
  170. // Call the chat method on the server
  171. chat.server.send($('#msg').val());
  172. });
  173. });
  174. });