Node.js socket.io和ui路由器更改状态
我发现了一些问题,但没有适当的解决办法。 我想完成一些理论上看起来很简单的事情: 我有两个视图(ui路由器状态):Node.js socket.io和ui路由器更改状态,node.js,socket.io,angular-ui-router,Node.js,Socket.io,Angular Ui Router,我发现了一些问题,但没有适当的解决办法。 我想完成一些理论上看起来很简单的事情: 我有两个视图(ui路由器状态): “列表”,显示带有“编辑”按钮的文档列表。单击按钮,您将进入编辑器 显示用于编辑单个文档的表单的“编辑器” 如果用户访问“编辑器”,我想向“列表”视图发送消息并禁用编辑按钮 因此,我尝试用一种简单的websocket方法来禁用对正在编辑的文档(仅在前端)的写访问 当我改变路线时,问题之一就出现了。插座停止工作了。这个想法是在我加入“列表”视图时打开一个套接字连接,在我离开它到任
- “列表”,显示带有“编辑”按钮的文档列表。单击按钮,您将进入编辑器
- 显示用于编辑单个文档的表单的“编辑器”李>
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();
});
我希望在服务器端保留的锁定文档的“实时”数组的格式如下:
lockedList = [
{"client":"TourJX6zem1MpCx8AAAH","document":"580e899ca9c8621c48c0042d"}
]
保留客户端socketId是必要的,因为我想不出更好的方法来解锁文档用户停止编辑(用户保存对文档的编辑或只关闭浏览器/选项卡而不保存)
“列表”角度控制器
socket.on('connect', function () {
socket.emit('doc:askList', function () {});
});
socket.on('user:get', function (clientid) {
console.log("(list)here your 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();
});
socket.on('connect', function () {
console.log("editor_connect");
});
socket.on('user:get', function (clientid) {
console.log("here your socked id: " + clientid);
$scope.clientid = clientid;
$scope.message = { client: clientid, document: $scope.model._id }; //$scope.model._id is the document id: once I have the client connection Id, I a emit doc:lock with the clientId and the documentId
socket.emit('doc:lock', $scope.message);
});
$scope.$on('$stateChangeStart', function (event, next) {
socket.disconnect();
});
$scope.$on('$destroy', function (event) {
socket.disconnect();
});
“编辑器”角度控制器
socket.on('connect', function () {
socket.emit('doc:askList', function () {});
});
socket.on('user:get', function (clientid) {
console.log("(list)here your 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();
});
socket.on('connect', function () {
console.log("editor_connect");
});
socket.on('user:get', function (clientid) {
console.log("here your socked id: " + clientid);
$scope.clientid = clientid;
$scope.message = { client: clientid, document: $scope.model._id }; //$scope.model._id is the document id: once I have the client connection Id, I a emit doc:lock with the clientId and the documentId
socket.emit('doc:lock', $scope.message);
});
$scope.$on('$stateChangeStart', function (event, next) {
socket.disconnect();
});
$scope.$on('$destroy', function (event) {
socket.disconnect();
});
最后一点注意:为了解决$apply问题,我使用此处发布的socketio angular Service我的问题中是否有不太清楚的地方?我的问题中是否有不太清楚的地方?