2015-07-11 00:43:08 -04:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var EventEmitter = require('events').EventEmitter;
|
|
|
|
var TextOperation = require('./text-operation');
|
|
|
|
var WrappedOperation = require('./wrapped-operation');
|
|
|
|
var Server = require('./server');
|
|
|
|
var Selection = require('./selection');
|
|
|
|
var util = require('util');
|
|
|
|
|
2015-07-14 11:38:51 -04:00
|
|
|
var logger = require('../logger');
|
2015-07-11 00:43:08 -04:00
|
|
|
|
2016-07-29 23:21:38 -04:00
|
|
|
function EditorSocketIOServer(document, operations, docId, mayWrite, operationCallback) {
|
2015-07-11 00:43:08 -04:00
|
|
|
EventEmitter.call(this);
|
|
|
|
Server.call(this, document, operations);
|
|
|
|
this.users = {};
|
|
|
|
this.docId = docId;
|
|
|
|
this.mayWrite = mayWrite || function (_, cb) {
|
|
|
|
cb(true);
|
|
|
|
};
|
2016-07-29 23:21:38 -04:00
|
|
|
this.operationCallback = operationCallback;
|
2015-07-11 00:43:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
util.inherits(EditorSocketIOServer, Server);
|
|
|
|
extend(EditorSocketIOServer.prototype, EventEmitter.prototype);
|
|
|
|
|
|
|
|
function extend(target, source) {
|
|
|
|
for (var key in source) {
|
|
|
|
if (source.hasOwnProperty(key)) {
|
|
|
|
target[key] = source[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
EditorSocketIOServer.prototype.addClient = function (socket) {
|
|
|
|
var self = this;
|
|
|
|
socket.join(this.docId);
|
|
|
|
var docOut = {
|
|
|
|
str: this.document,
|
|
|
|
revision: this.operations.length,
|
|
|
|
clients: this.users
|
|
|
|
};
|
2017-01-01 21:52:47 -05:00
|
|
|
socket.emit('doc', docOut);
|
2015-07-11 00:43:08 -04:00
|
|
|
socket.on('operation', function (revision, operation, selection) {
|
2016-01-17 10:51:27 -05:00
|
|
|
socket.origin = 'operation';
|
2015-07-11 00:43:08 -04:00
|
|
|
self.mayWrite(socket, function (mayWrite) {
|
|
|
|
if (!mayWrite) {
|
|
|
|
console.log("User doesn't have the right to edit.");
|
|
|
|
return;
|
|
|
|
}
|
2015-07-14 11:38:51 -04:00
|
|
|
try {
|
|
|
|
self.onOperation(socket, revision, operation, selection);
|
2016-07-29 23:21:38 -04:00
|
|
|
if (typeof self.operationCallback === 'function')
|
|
|
|
self.operationCallback(socket, operation);
|
2015-07-14 11:38:51 -04:00
|
|
|
} catch (err) {
|
2016-10-10 08:40:45 -04:00
|
|
|
setTimeout(function() {
|
|
|
|
var docOut = {
|
|
|
|
str: self.document,
|
|
|
|
revision: self.operations.length,
|
|
|
|
clients: self.users,
|
|
|
|
force: true
|
|
|
|
};
|
2017-01-01 21:52:47 -05:00
|
|
|
socket.emit('doc', docOut);
|
2016-10-10 08:40:45 -04:00
|
|
|
}, 100);
|
2015-07-14 11:38:51 -04:00
|
|
|
}
|
2015-07-11 00:43:08 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
socket.on('get_operations', function (base, head) {
|
|
|
|
self.onGetOperations(socket, base, head);
|
|
|
|
});
|
|
|
|
socket.on('selection', function (obj) {
|
2016-01-17 10:51:27 -05:00
|
|
|
socket.origin = 'selection';
|
2015-07-11 00:43:08 -04:00
|
|
|
self.mayWrite(socket, function (mayWrite) {
|
|
|
|
if (!mayWrite) {
|
|
|
|
console.log("User doesn't have the right to edit.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
self.updateSelection(socket, obj && Selection.fromJSON(obj));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
socket.on('disconnect', function () {
|
|
|
|
//console.log("Disconnect");
|
|
|
|
socket.leave(self.docId);
|
|
|
|
self.onDisconnect(socket);
|
|
|
|
/*
|
|
|
|
if (socket.manager && socket.manager.sockets.clients(self.docId).length === 0) {
|
|
|
|
self.emit('empty-room');
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
EditorSocketIOServer.prototype.onOperation = function (socket, revision, operation, selection) {
|
|
|
|
var wrapped;
|
|
|
|
try {
|
|
|
|
wrapped = new WrappedOperation(
|
|
|
|
TextOperation.fromJSON(operation),
|
|
|
|
selection && Selection.fromJSON(selection)
|
|
|
|
);
|
|
|
|
} catch (exc) {
|
2015-07-14 11:38:51 -04:00
|
|
|
logger.error("Invalid operation received: ");
|
|
|
|
logger.error(exc);
|
|
|
|
throw new Error(exc);
|
2015-07-11 00:43:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
var clientId = socket.id;
|
|
|
|
var wrappedPrime = this.receiveOperation(revision, wrapped);
|
2015-07-16 10:46:06 -04:00
|
|
|
if(!wrappedPrime) return;
|
2015-07-11 00:43:08 -04:00
|
|
|
//console.log("new operation: " + JSON.stringify(wrapped));
|
|
|
|
this.getClient(clientId).selection = wrappedPrime.meta;
|
|
|
|
revision = this.operations.length;
|
|
|
|
socket.emit('ack', revision);
|
|
|
|
socket.broadcast.in(this.docId).emit(
|
|
|
|
'operation', clientId, revision,
|
|
|
|
wrappedPrime.wrapped.toJSON(), wrappedPrime.meta
|
|
|
|
);
|
2016-01-17 10:51:27 -05:00
|
|
|
//set document is dirty
|
2015-07-11 00:43:08 -04:00
|
|
|
this.isDirty = true;
|
|
|
|
} catch (exc) {
|
2015-07-14 11:38:51 -04:00
|
|
|
logger.error(exc);
|
|
|
|
throw new Error(exc);
|
2015-07-11 00:43:08 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
EditorSocketIOServer.prototype.onGetOperations = function (socket, base, head) {
|
|
|
|
var operations = this.operations.slice(base, head).map(function (op) {
|
|
|
|
return op.wrapped.toJSON();
|
|
|
|
});
|
|
|
|
socket.emit('operations', head, operations);
|
|
|
|
};
|
|
|
|
|
|
|
|
EditorSocketIOServer.prototype.updateSelection = function (socket, selection) {
|
|
|
|
var clientId = socket.id;
|
|
|
|
if (selection) {
|
|
|
|
this.getClient(clientId).selection = selection;
|
|
|
|
} else {
|
|
|
|
delete this.getClient(clientId).selection;
|
|
|
|
}
|
|
|
|
socket.broadcast.to(this.docId).emit('selection', clientId, selection);
|
|
|
|
};
|
|
|
|
|
|
|
|
EditorSocketIOServer.prototype.setName = function (socket, name) {
|
|
|
|
var clientId = socket.id;
|
|
|
|
this.getClient(clientId).name = name;
|
|
|
|
socket.broadcast.to(this.docId).emit('set_name', clientId, name);
|
|
|
|
};
|
|
|
|
|
|
|
|
EditorSocketIOServer.prototype.setColor = function (socket, color) {
|
|
|
|
var clientId = socket.id;
|
|
|
|
this.getClient(clientId).color = color;
|
|
|
|
socket.broadcast.to(this.docId).emit('set_color', clientId, color);
|
|
|
|
};
|
|
|
|
|
|
|
|
EditorSocketIOServer.prototype.getClient = function (clientId) {
|
|
|
|
return this.users[clientId] || (this.users[clientId] = {});
|
|
|
|
};
|
|
|
|
|
|
|
|
EditorSocketIOServer.prototype.onDisconnect = function (socket) {
|
|
|
|
var clientId = socket.id;
|
|
|
|
delete this.users[clientId];
|
|
|
|
socket.broadcast.to(this.docId).emit('client_left', clientId);
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = EditorSocketIOServer;
|