I am new to express.io.I have a set of code running in socket.io with node.js and angular in frontend with no problem.I have created server and it is emitting a set of data in an interval of 1000ms.Sample code for the server which is interacting with client is
var serverjson = [
{"Product": "REL", "BBP": "10", "BSP": "10.2", "LTP": "10.1" },
{"Product": "BEL", "BBP": "20", "BSP": "20.4", "LTP": "20" },
{"Product": "MTL", "BBP": "50", "BSP": "50.5", "LTP": "50.1" },
{"Product": "BSL", "BBP": "100", "BSP": "101", "LTP": "100.2" }
];
// define interactions with client
app.io.route('ready', function(req){
//send data to client
setInterval(function(){
for(i=0;i<serverjson.length;i++)
{
serverjson[i].BBP = Math.round((parseInt(serverjson[i].BBP) + Math.random())*100)/100;
serverjson[i].BSP = Math.round((parseInt(serverjson[i].BSP) + Math.random())*100)/100;
serverjson[i].LTP = Math.round((parseInt(serverjson[i].LTP) + Math.random())*100)/100;
}
var serverjsonstr = JSON.stringify(serverjson);
req.io.emit('msg', {'msg': serverjsonstr});
}, 1000);
});
and in the client side
<!doctype html>
<html>
<head>
<title>express.io Test</title>
<script src="/http://ift.tt/1aeIZU4"></script>
<script src="http://ift.tt/1pMvisQ"></script>
</head>
<body>
<script>
var io = io.connect();
io.emit('ready');
io.on('msg', function(data){
$('#msg').text(data.msg);
});
</script>
<div id="msg"></div>
</body>
</html>
upto this everything is running with problem.data is showing properly with the interval.But in another page I want to show the data with angular controller. I have done the same example in socket.io.For the controller the code was
var apps = angular.module('stocksample', []);
apps.controller('StockListCtrl', function($scope, socket) {
$scope.stocks = [];
socket.on('msg', function(data) {
$scope.stocks = JSON.parse(data.msg);
});
});
apps.factory('socket', function($rootScope) {
var socket = io.connect();
return {
on: function(eventName, callback) {
socket.on(eventName, function() {
var args = arguments;
$rootScope.$apply(function() {
callback.apply(socket, args);
});
});
},
};
});
and that run well for socket.io which was binding data for stocks whenever io.emit for 'msg' was taking place and showing in some other page.I need to code for the same controller with express.io.Any suggestion please.Help will be greatly appreciated.
Aucun commentaire:
Enregistrer un commentaire