PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/public/javascript/pump/socket.js

https://gitlab.com/fuzzynemesis/pump.io
JavaScript | 169 lines | 103 code | 44 blank | 22 comment | 13 complexity | 12c814bac7eb832f9254d67fe8fd2683 MD5 | raw file
Possible License(s): Apache-2.0
  1. // pump/socket.js
  2. //
  3. // Socket module for the pump.io client UI
  4. //
  5. // Copyright 2011-2012, E14N https://e14n.com/
  6. //
  7. // Licensed under the Apache License, Version 2.0 (the "License");
  8. // you may not use this file except in compliance with the License.
  9. // You may obtain a copy of the License at
  10. //
  11. // http://www.apache.org/licenses/LICENSE-2.0
  12. //
  13. // Unless required by applicable law or agreed to in writing, software
  14. // distributed under the License is distributed on an "AS IS" BASIS,
  15. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. // See the License for the specific language governing permissions and
  17. // limitations under the License.
  18. (function(_, $, Backbone, Pump) {
  19. Pump.getStreams = function() {
  20. var streams = {};
  21. if (Pump.body) {
  22. if (Pump.body.content) {
  23. _.extend(streams, Pump.body.content.getStreams());
  24. }
  25. if (Pump.body.nav) {
  26. _.extend(streams, Pump.body.nav.getStreams());
  27. }
  28. }
  29. return streams;
  30. };
  31. // Refreshes the current visible streams
  32. Pump.refreshStreams = function() {
  33. var streams = Pump.getStreams();
  34. _.each(streams, function(stream, name) {
  35. stream.getPrev();
  36. });
  37. };
  38. Pump.updateStream = function(url, activity) {
  39. var streams = Pump.getStreams(),
  40. target = _.find(streams, function(stream) { return stream.url() == url; }),
  41. act;
  42. if (target) {
  43. act = Pump.Activity.unique(activity);
  44. target.items.unshift(act);
  45. }
  46. };
  47. // When we get a challenge from the socket server,
  48. // We prepare an OAuth request and send it
  49. Pump.riseToChallenge = function(url, method) {
  50. var message = {action: url,
  51. method: method,
  52. parameters: [["oauth_version", "1.0"]]};
  53. Pump.ensureCred(function(err, cred) {
  54. var pair, secrets;
  55. if (err) {
  56. Pump.error("Error getting OAuth credentials.");
  57. return;
  58. }
  59. message.parameters.push(["oauth_consumer_key", cred.clientID]);
  60. secrets = {consumerSecret: cred.clientSecret};
  61. pair = Pump.getUserCred();
  62. if (pair) {
  63. message.parameters.push(["oauth_token", pair.token]);
  64. secrets.tokenSecret = pair.secret;
  65. }
  66. OAuth.setTimestampAndNonce(message);
  67. OAuth.SignatureMethod.sign(message, secrets);
  68. Pump.socket.send(JSON.stringify({cmd: "rise", message: message}));
  69. });
  70. };
  71. // Our socket.io socket
  72. Pump.socket = null;
  73. Pump.setupSocket = function() {
  74. var here = window.location,
  75. sock;
  76. if (Pump.socket) {
  77. Pump.socket.close();
  78. Pump.socket = null;
  79. }
  80. sock = new SockJS(here.protocol + "//" + here.host + "/main/realtime/sockjs");
  81. sock.onopen = function() {
  82. Pump.socket = sock;
  83. Pump.followStreams();
  84. };
  85. sock.onmessage = function(e) {
  86. var data = JSON.parse(e.data);
  87. switch (data.cmd) {
  88. case "update":
  89. Pump.updateStream(data.url, data.activity);
  90. break;
  91. case "challenge":
  92. Pump.riseToChallenge(data.url, data.method);
  93. break;
  94. }
  95. };
  96. sock.onclose = function() {
  97. // XXX: reconnect?
  98. Pump.socket = null;
  99. };
  100. };
  101. Pump.followStreams = function() {
  102. if (!Pump.config.sockjs) {
  103. return;
  104. }
  105. if (!Pump.socket) {
  106. return;
  107. }
  108. var streams = Pump.getStreams();
  109. _.each(streams, function(stream, name) {
  110. Pump.socket.send(JSON.stringify({cmd: "follow", url: stream.url()}));
  111. });
  112. };
  113. Pump.unfollowStreams = function() {
  114. if (!Pump.config.sockjs) {
  115. return;
  116. }
  117. if (!Pump.socket) {
  118. return;
  119. }
  120. var streams = Pump.getStreams();
  121. _.each(streams, function(stream, name) {
  122. Pump.socket.send(JSON.stringify({cmd: "unfollow", url: stream.url()}));
  123. });
  124. };
  125. })(window._, window.$, window.Backbone, window.Pump);