not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

302 lines
8.0 KiB

  1. define(function (require) {
  2. // Message type constants
  3. var msgInit = 0;
  4. var msgListUsers = 1;
  5. var msgCreateSharedActivity = 2;
  6. var msgListSharedActivities = 3;
  7. var msgJoinSharedActivity = 4;
  8. var msgLeaveSharedActivity = 5;
  9. var msgOnConnectionClosed = 6;
  10. var msgOnSharedActivityUserChanged = 7;
  11. var msgSendMessage = 8;
  12. var msgListSharedActivityUsers = 9;
  13. // Array for callbacks on each type
  14. var callbackArray = [];
  15. // User and shared info storage
  16. var userInfo = null;
  17. var sharedInfo = null;
  18. // Connection object
  19. function SugarPresence() {
  20. // Init callbacks
  21. var emptyCallback = function() {};
  22. var listUsersCallback = emptyCallback;
  23. var createSharedActivityCallback = emptyCallback;
  24. var listSharedActivityCallback = emptyCallback;
  25. var joinSharedActivity = emptyCallback;
  26. var leaveSharedActivity = emptyCallback;
  27. var onConnectionClosed = emptyCallback;
  28. var onSharedActivityUserChanged = emptyCallback;
  29. var receivedDataCallback = emptyCallback;
  30. var listSharedActivityUsersCallback = emptyCallback;
  31. callbackArray = [emptyCallback, listUsersCallback, createSharedActivityCallback,
  32. listSharedActivityCallback, joinSharedActivity, leaveSharedActivity,
  33. onConnectionClosed, onSharedActivityUserChanged, receivedDataCallback,
  34. listSharedActivityUsersCallback
  35. ];
  36. this.socket = null;
  37. // Handle message received from server
  38. this.registerMessageHandler = function() {
  39. // Get message content
  40. this.socket.onmessage = function(event) {
  41. // Convert message to JSON
  42. var edata = event.data;
  43. try {
  44. var json = JSON.parse(edata);
  45. } catch (e) {
  46. console.log('Presence API error, this doesn\'t look like a valid JSON: ', edata);
  47. return;
  48. }
  49. // Call the matching callback
  50. if (json.type < callbackArray.length)
  51. callbackArray[json.type](json.data);
  52. else
  53. console.log('Presence API error, unknown callback type:'+json.type);
  54. };
  55. }
  56. // Register user to the server
  57. this.registerUser = function() {
  58. this.socket.send(JSON.stringify(this.userInfo));
  59. }
  60. }
  61. // Create presence object
  62. var presence = new SugarPresence();
  63. // Test if connected to network
  64. SugarPresence.prototype.isConnected = function() {
  65. return (this.socket != null && this.socket.readyState == 1);
  66. }
  67. // Get user info
  68. SugarPresence.prototype.getUserInfo = function() {
  69. return this.userInfo;
  70. }
  71. // Get shared activity info
  72. SugarPresence.prototype.getSharedInfo = function() {
  73. return this.sharedInfo;
  74. }
  75. // Get settings
  76. function getSugarSettings(callback) {
  77. if (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) {
  78. chrome.storage.local.get('sugar_settings', function(values) {
  79. callback(values.sugar_settings);
  80. });
  81. } else {
  82. callback(localStorage.sugar_settings);
  83. }
  84. }
  85. // Join network function
  86. SugarPresence.prototype.joinNetwork = function(callback) {
  87. // Check WebSocket support
  88. if (!window.WebSocket){
  89. that.socket = null;
  90. console.log('WebSocket not supported');
  91. callback({code: -1}, presence);
  92. }
  93. // Get server settings
  94. var that = this;
  95. getSugarSettings(function (sugar_settings) {
  96. // Get server name
  97. var server = location.hostname;
  98. var port = 8039;
  99. var secure = false;
  100. if (sugar_settings) {
  101. var sugarSettings = JSON.parse(sugar_settings);
  102. if (sugarSettings.server) {
  103. server = sugarSettings.server.url.substring(sugarSettings.server.url.indexOf("://")+3);
  104. secure = (sugarSettings.server.url.indexOf("https://") == 0);
  105. var endName = server.indexOf(':')
  106. if (endName == -1) endName = server.indexOf('/');
  107. if (endName == -1) endName = server.length;
  108. server = server.substring(0, endName);
  109. port = sugarSettings.server.presence;
  110. }
  111. }
  112. // Connect to server
  113. if (!server) {
  114. that.socket = null;
  115. console.log('No server available');
  116. callback({code: -2}, presence);
  117. return;
  118. }
  119. that.socket = new WebSocket((secure ? 'wss://' : 'ws://')+server+':'+port);
  120. that.socket.onerror = function(error) {
  121. console.log('WebSocket Error: ' + error);
  122. callback(error, presence);
  123. that.socket = null;
  124. };
  125. // When connection open, send user info
  126. that.socket.onopen = function(event) {
  127. var sugarSettings = JSON.parse(sugar_settings);
  128. that.userInfo = {
  129. name: sugarSettings.name,
  130. networkId: sugarSettings.networkId,
  131. colorvalue: sugarSettings.colorvalue
  132. };
  133. that.registerMessageHandler();
  134. that.registerUser();
  135. callback(null, presence);
  136. };
  137. // When connection closed, call closed callback
  138. that.socket.onclose = function(event) {
  139. callbackArray[msgOnConnectionClosed](event);
  140. };
  141. });
  142. }
  143. // Leave network
  144. SugarPresence.prototype.leaveNetwork = function() {
  145. if (!this.isConnected())
  146. return;
  147. this.socket.close();
  148. this.socket = null;
  149. }
  150. // List all users. Will receive an array of users.
  151. SugarPresence.prototype.listUsers = function(callback) {
  152. if (!this.isConnected())
  153. return;
  154. // Register call back
  155. callbackArray[msgListUsers] = callback;
  156. // Send list user message
  157. var sjson = JSON.stringify({
  158. type: msgListUsers
  159. });
  160. this.socket.send(sjson);
  161. }
  162. // List users connected to a group. Will receive an array of users.
  163. SugarPresence.prototype.listSharedActivityUsers = function(group, callback) {
  164. if (!this.isConnected())
  165. return;
  166. // Register call back
  167. var that = this;
  168. callbackArray[msgListSharedActivityUsers] = function(data) {
  169. callback(data);
  170. }
  171. // Send list user message
  172. var sjson = JSON.stringify({
  173. type: msgListSharedActivityUsers,
  174. group: group
  175. });
  176. this.socket.send(sjson);
  177. }
  178. // Create a shared activity. Will receive a unique group id.
  179. SugarPresence.prototype.createSharedActivity = function(activityId, callback) {
  180. if (!this.isConnected())
  181. return;
  182. // Register call back
  183. var that = this;
  184. callbackArray[msgCreateSharedActivity] = function(data) {
  185. that.sharedInfo = { id: data };
  186. callback(data);
  187. }
  188. // Send create shared activity message
  189. var sjson = JSON.stringify({
  190. type: msgCreateSharedActivity,
  191. activityId: activityId
  192. });
  193. this.socket.send(sjson);
  194. }
  195. // List all shared activities. Will receive an array of each shared activities and users connected
  196. SugarPresence.prototype.listSharedActivities = function(callback) {
  197. if (!this.isConnected())
  198. return;
  199. // Register call back
  200. callbackArray[msgListSharedActivities] = callback;
  201. // Send list shared activities message
  202. var sjson = JSON.stringify({
  203. type: msgListSharedActivities
  204. });
  205. this.socket.send(sjson);
  206. }
  207. // Join a shared activity. Will receive group properties or null
  208. SugarPresence.prototype.joinSharedActivity = function(group, callback) {
  209. if (!this.isConnected())
  210. return;
  211. // Register call back
  212. var that = this;
  213. callbackArray[msgJoinSharedActivity] = function(data) {
  214. that.sharedInfo = data;
  215. callback(data);
  216. }
  217. // Send join shared activity message
  218. var sjson = JSON.stringify({
  219. type: msgJoinSharedActivity,
  220. group: group
  221. });
  222. this.socket.send(sjson);
  223. }
  224. // Leave shared activities
  225. SugarPresence.prototype.leaveSharedActivity = function(group, callback) {
  226. if (!this.isConnected())
  227. return;
  228. // Register call back
  229. callbackArray[msgLeaveSharedActivity] = callback; // BUG: This callback is never called
  230. // Send leave shared activity message
  231. var sjson = JSON.stringify({
  232. type: msgLeaveSharedActivity,
  233. group: group
  234. });
  235. this.socket.send(sjson);
  236. }
  237. // Register connection closed event
  238. SugarPresence.prototype.onConnectionClosed = function(callback) {
  239. callbackArray[msgOnConnectionClosed] = callback;
  240. }
  241. // Register shared activity user changed event
  242. SugarPresence.prototype.onSharedActivityUserChanged = function(callback) {
  243. callbackArray[msgOnSharedActivityUserChanged] = callback;
  244. }
  245. // Send message to a group
  246. SugarPresence.prototype.sendMessage = function(group, data) {;
  247. if (!this.isConnected())
  248. return;
  249. var sjson = JSON.stringify({
  250. type: msgSendMessage,
  251. group: group,
  252. data: data
  253. });
  254. this.socket.send(sjson);
  255. }
  256. // Register data received message
  257. SugarPresence.prototype.onDataReceived = function(callback) {
  258. callbackArray[msgSendMessage] = callback;
  259. }
  260. return presence;
  261. });