Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- angular.module('chat', ['ngWebsocket'])
- .config(function($interpolateProvider, $locationProvider) {
- $interpolateProvider.startSymbol('[[');
- $interpolateProvider.endSymbol(']]');
- $locationProvider.html5Mode({enabled: true, requireBase: false}).hashPrefix('!');
- })
- .run(['$anchorScroll', function($anchorScroll){
- $anchorScroll.yOffset = 50;
- }])
- .controller('ChatMessages', ['$scope', '$filter', '$anchorScroll', '$location', '$http', '$websocket', function($scope, $filter, $anchorScroll, $location, $http, $websocket){
- $scope.messages = [];
- $scope.newMessage = "";
- $scope.msgCount = 0;
- console.log($location.path());
- $scope.ws = $websocket.$new('ws://localhost:9000' + $location.path() + '/ws');
- $scope.ws.$on('$message', function(data) {
- console.log(data);
- if (data.event == 'message') {
- $scope.addMessage(data.data);
- $scope.gotoBottom();
- }
- });
- $scope.addMessage = function (message) {
- message.Datestr = $filter('date')(new Date(message.Timestamp*1000), 'dd.MM.yyyy');
- message.hash = $scope.msgCount;
- $scope.messages.push(message);
- $scope.$apply();
- };
- $scope.$watchCollection('messages', function(newMsgs, oldMsges){
- $scope.msgCount = newMsgs.length;
- });
- $scope.send = function() {
- if ($scope.newMessage != "") {
- $scope.ws.$emit('message', $scope.newMessage);
- $scope.newMessage = "";
- }
- };
- $scope.gotoBottom = function() {
- $location.hash('bottom');
- $anchorScroll();
- };
- }]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement