Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var myApp = angular.module('mainController',[]);
- myApp.controller('mainController', ['$scope','$http','$state', function($scope,$http,$state) {
- /* Data required for app to use */
- $scope.messagess= new Array();
- $scope.loginData = {}; // data for login form
- $scope.registrationData = {};
- /*----------------Connecting to spring controller /socket/recive and retriving data on changes--------*/
- if (typeof(EventSource) !== "undefined") {
- // Yes! Server-sent events support!
- var source = new EventSource('api/socket/recive');
- $scope.sourceListener=source;
- $scope.sourceListener.addEventListener('recived-message',function(event){
- var data = JSON.parse(event.data);
- message = {
- recived : true,
- text: data.message,
- from: 'Server',
- time: '3.1.2016'
- }
- $scope.$apply(function() {
- $scope.messagess.push(message);
- })
- });
- $scope.sourceListener.onmessage = function (event) {
- // a message without a type was fired
- };
- }
- else {
- alert('SSE not supported by browser.');
- }
- $scope.sendMessage = function() {
- var text = $scope.text.text;
- if(text!=null && text!=''){
- var message = new Object();
- message = {
- recived : false,
- text: text,
- from: 'Me',
- time: '3.1.2016'
- }
- $scope.messagess.push(message);
- $scope.text.text = "";
- $http.get("api/socket?message="+text);
- }
- }
- var insertFriendsMessage=function(data){
- }
- $scope.signout = function(){
- $http({
- method: 'GET',
- url: 'api/socket/disconect'
- }).then(function successCallback(response) {
- $state.go("login");
- }, function errorCallback(response) {
- console.log(response);
- });
- }
- /*---------------LOGIN PART--------------------*/
- $scope.login = function() {
- console.log($scope.loginData);
- $state.go("main");
- }
- /*------------LOGIN PART END---------------------*/
- /*------------REGISTRATION PART------------------*/
- $scope.register = function() {
- var data = {
- username : $scope.registrationData.username,
- email : $scope.registrationData.email,
- password : $scope.registrationData.password,
- avatar : null
- };
- console.log(data);
- $http({
- method: 'POST',
- url: 'http://thedevspot.xyz:90/user/insert',
- data: data
- }).then(function successCallback(response) {
- console.log(response);
- $state.go("login");
- }, function errorCallback(response) {
- console.log(response);
- });
- }
- /*------------REGISTRATION PART END------------------*/
- }]);
Add Comment
Please, Sign In to add comment