Did you ever fix this problem? On Thursday, April 18, 2013 2:11:50 PM UTC-6, AnonShortTerm wrote: > > @Gal Ben-Haim:: Did you ever solve the problem of having the listeners > not be destroyed? I noticed your > stackedoverflow<http://stackoverflow.com/questions/14389049/how-to-use-angularjs-with-socket-io>question. > I get lost on the definition of the function > removeListener. > > I could rewrite it to work as Peter Bacon Darwin, but then i would have to > write more and create getters for the information. Ill do it if must, but a > solution to the above problem would be grand. > > > On Thursday, January 17, 2013 5:16:40 PM UTC-5, Gal Ben-Haim wrote: >> >> I want to use socket.io in AngularJS. I found the following factory: >> >> app.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); >> }); >> }); >> }, >> emit: function (eventName, data, callback) { >> socket.emit(eventName, data, function () { >> var args = arguments; >> $rootScope.$apply(function () { >> if (callback) { >> callback.apply(socket, args); >> } >> }); >> }) >> } >> }; >> >> and it is used in the controller like: >> >> function MyCtrl($scope, socket) { >> socket.on('message', function(data) { >> ... >> }); >> }; >> >> the problem is that each time the controller is visited another listener >> is added, so when a message is received it is handled multiple times. >> >> what can be a better strategy to integrate socket.io with AngularJS ? >> >> On Saturday, June 23, 2012 2:43:25 AM UTC+3, mind wrote: >>> >>> you can wrap original socket.emit and socket.on in $rootScope.$apply, so >>> you don't have to write it each time. >>> http://jsfiddle.net/Ysvn4/1/ no references to $scope and it keeps >>> semantics of socket.io >>> socket._on(e, function(data) { >>> // changes here are propagated to views >>> }) >>> socket._emit(e, function responseHandler(data) { >>> // this is called inside $rootScope.$apply >>> }) >>> >>
-- You received this message because you are subscribed to the Google Groups "AngularJS" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/angular. For more options, visit https://groups.google.com/groups/opt_out.
