node.js - socket.io and ui-router change state -


i found question this, no proper solution. want accomplish looks theoretically simple: have 2 views (ui-router states):

  • "list" shows list of documents "edit" button. clicking on button go editor.
  • "editor" shows form edits single document.

if user visits 'editor' want send message "list" view , disable edit button.

so i'm trying simple websocket way disable access in write documents in editing (just in frontend).

one of problems come when change route. socket stop working. idea open socket connection when join "list" view , disconnect when leave other route (including "editor"); same "editor" view. below code, doesn't work. following idea, when user clicks on edit button (leave 'list' view join 'editor' view) have deal rapid disconnection (list view)-connection(editor view) in immediate sequence (i guess cannot sure sequence of socket events..)

server-side

var lockedlist = []; io.on('connection', function (socket) {   console.log('a user connected:'+socket.id);   socket.emit('user:get',socket.id);   socket.emit('doc:list', lockedlist);   socket.on('disconnect', function (s) {     console.error('a user disconnected:'+socket.id);     lockedlist = lockedlist.filter(function(el) {       return el.client !== socket.id;     });     socket.broadcast.emit('doc:list',lockedlist);     socket.removealllisteners('doc:list'); //?     socket.removealllisteners('disconnect');   });    socket.on('doc:lock', function (obj) {     console.error('doc:lock:');     console.error(obj);     lockedlist.push(obj);     socket.broadcast.emit('doc:list',lockedlist);   });    socket.on('doc:unlock', function (obj) {     console.error('doc:unlock:');     console.error(obj);     lockedlist = lockedlist.filter(function(el) {       return el.client !== socket.id;     });     socket.broadcast.emit('doc:list',lockedlist);     socket.disconnect();   });   socket.on('doc:list', function () {     console.log('doc:list')     socket.broadcast.emit('doc:list',lockedlist);   });   socket.on('doc:asklist', function () {     console.log('doc:asklist')     socket.broadcast.emit('doc:list',lockedlist);   });   socket.on('user:disconnect', function () {     console.log('user:disconnect')     socket.broadcast.emit('doc:list',lockedlist);     socket.disconnect();    }); 

the "live" array of locked documents want keep server-side in form:

lockedlist = [       {"client":"tourjx6zem1mpcx8aaah","document":"580e899ca9c8621c48c0042d"}  ] 

keep client socketid necessary because can't figure out better way unlock document user stop editing (the user save edits document or close browser/tab without save).

"list" angular controller

socket.on('connect', function () {     socket.emit('doc:asklist', function () {}); }); socket.on('user:get', function (clientid) {     console.log("(list)here socked id: " + clientid); }); socket.on('doc:list', function (list) {     $scope.lockedlist = list; }); $scope.$on('$statechangestart', function (event, next) {     socket.disconnect(); }); $scope.$on('$destroy', function (event) {     socket.disconnect(); }); 

"editor" angular controller

socket.on('connect', function () {     console.log("editor_connect"); }); socket.on('user:get', function (clientid) {     console.log("here socked id: " + clientid);     $scope.clientid = clientid;     $scope.message = { client: clientid, document: $scope.model._id }; //$scope.model._id document id: once have client connection id, emit doc:lock clientid , documentid     socket.emit('doc:lock', $scope.message); }); $scope.$on('$statechangestart', function (event, next) {     socket.disconnect(); }); $scope.$on('$destroy', function (event) {     socket.disconnect(); }); 

last note: solve $apply issue, use socketio angular service posted here https://stackoverflow.com/a/17092745/3671995


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -