SHARE
TWEET

Untitled

a guest May 12th, 2016 11 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // factory
  2.  
  3. .factory('Messages', function($websocket , $stateParams) {
  4.   var baseURL = 'ws://10.0.0.16:8000/ws/';
  5.   var channel = $stateParams.chatId;
  6.   var echo = '?subscribe-broadcast&publish-broadcast&echo';
  7.  
  8.   var ws = $websocket(baseURL + channel + echo);
  9.   var collection = [];
  10.  
  11.   ws.onMessage(function(event) {
  12.     var res;
  13.     try {
  14.       res = JSON.parse(event.data);
  15.     } catch(e) {
  16.       res = { 'content': event.data };
  17.     }
  18.     collection.push(
  19.       // { content: res.content }
  20.       JSON.parse(event.data)
  21.     );
  22.   });
  23.  
  24.   ws.onError(function(event) {
  25.     console.log('connection error', event);
  26.   });
  27.  
  28.   ws.onClose(function(event) {
  29.     console.log('connection closed', event);
  30.   });
  31.  
  32.   return {
  33.     test: function(data) {
  34.       var ws = $websocket(baseURL + data + echo);
  35.       return ws.onOpen();
  36.     },
  37.     close: function() {
  38.       return ws.close();
  39.     },
  40.     collection: collection,
  41.     status: function() {
  42.       return ws.readyState;
  43.     },
  44.     send: function(content) {
  45.       if (angular.isString(content)) {
  46.         ws.send(content);
  47.       }
  48.       else if (angular.isObject(content)) {
  49.         ws.send(JSON.stringify(content));
  50.       }
  51.     }
  52.   };
  53.  
  54. })
  55.  
  56.  
  57. //controller
  58.  
  59.   $scope.Messages = Messages;
  60.   $scope.sendMessage = function (new_message) {
  61.     if (!new_message) { return; }
  62.     Messages.send({
  63.       type: 'message',
  64.       //user: ChatApp.chat.user.id,
  65.       //avatar: ChatApp.chat.user.avatar,
  66.       //apiKey: ChatApp.chat.appKey,
  67.       chat: $stateParams.chatId,
  68.       is_native: false,
  69.       //is_read: status
  70.       content: new_message,
  71.       timestamp: event.timeStamp
  72.     });
  73.     $scope.new_message = '';
  74.   };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top