Advertisement
Guest User

Untitled

a guest
Mar 30th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. angular.module('chat', ['ngWebsocket'])
  2. .config(function($interpolateProvider, $locationProvider) {
  3.     $interpolateProvider.startSymbol('[[');
  4.     $interpolateProvider.endSymbol(']]');
  5.     $locationProvider.html5Mode({enabled: true, requireBase: false}).hashPrefix('!');
  6.   })
  7.  
  8. .run(['$anchorScroll', function($anchorScroll){
  9.     $anchorScroll.yOffset = 50;
  10. }])
  11.  
  12. .controller('ChatMessages', ['$scope', '$filter', '$anchorScroll', '$location', '$http', '$websocket', function($scope, $filter, $anchorScroll, $location, $http, $websocket){
  13.     $scope.messages = [];
  14.     $scope.newMessage = "";
  15.     $scope.msgCount = 0;
  16.     console.log($location.path());
  17.     $scope.ws = $websocket.$new('ws://localhost:9000' + $location.path() + '/ws');
  18.     $scope.ws.$on('$message', function(data) {
  19.             console.log(data);
  20.             if (data.event == 'message') {
  21.                 $scope.addMessage(data.data);
  22.                 $scope.gotoBottom();       
  23.             }
  24.         });
  25.  
  26.     $scope.addMessage = function (message) {
  27.         message.Datestr = $filter('date')(new Date(message.Timestamp*1000), 'dd.MM.yyyy');
  28.         message.hash = $scope.msgCount;
  29.         $scope.messages.push(message);
  30.         $scope.$apply();
  31.     };
  32.  
  33.     $scope.$watchCollection('messages', function(newMsgs, oldMsges){
  34.         $scope.msgCount = newMsgs.length;
  35.     });
  36.  
  37.     $scope.send = function() {
  38.         if ($scope.newMessage != "") {
  39.             $scope.ws.$emit('message', $scope.newMessage);
  40.             $scope.newMessage = "";
  41.         }
  42.     };
  43.  
  44.     $scope.gotoBottom = function() {
  45.         $location.hash('bottom');
  46.         $anchorScroll();
  47.     };
  48. }]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement