I am using web socket to connect to server. I am calling a service from controller. The request is going to server and response is coming back to service which is in app.js file.
Now I need the response in controller file.
Can any one help me how to send the response from app.js to controller from where the request is made.
app.js
app.factory('MyService', ['$rootScope', function($rootScope) {
var Service = { };
// Create our websocket object with the address to the websocket
var ws = new WebSocket("Server_URL");
ws.onopen = function(){
console.log("Socket has been opened!");
};
ws.onmessage = function(message) {
listener(message.data);
};
function sendRequest(request) {
console.log('Sending request:', request);
ws.send(request);
}
function listener(data) {
var messageObj = data;
console.log("Received data from websocket: ", messageObj);
}
Service.getTemp = function(request) {
sendRequest(request);
}
return Service;
}])
controller.js
app.controller('myController', function($scope, $state, $rootScope,MyService) {
$scope.currentTemp = MyService.getTemp('requestString');
console.log( $scope.currentTemp );
});