diff options
author | Wu Cheng-Han | 2015-07-11 12:43:08 +0800 |
---|---|---|
committer | Wu Cheng-Han | 2015-07-11 12:43:08 +0800 |
commit | 556338a9c6964d110c1351a402b425c71c2571fa (patch) | |
tree | d5b6d2071e554e65c7bfaa4f2c84ddb034598e01 /public/vendor/ot/socketio-adapter.js | |
parent | 4702b83adc35f384e214a2a6e9199d08e4494093 (diff) |
Added support of operational transformation
Diffstat (limited to 'public/vendor/ot/socketio-adapter.js')
-rwxr-xr-x | public/vendor/ot/socketio-adapter.js | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/public/vendor/ot/socketio-adapter.js b/public/vendor/ot/socketio-adapter.js new file mode 100755 index 00000000..329d4f3e --- /dev/null +++ b/public/vendor/ot/socketio-adapter.js @@ -0,0 +1,66 @@ +/*global ot */ + +ot.SocketIOAdapter = (function () { + 'use strict'; + + function SocketIOAdapter(socket) { + this.socket = socket; + + var self = this; + socket.on('client_left', function (clientId) { + self.trigger('client_left', clientId); + }); + socket.on('set_name', function (clientId, name) { + self.trigger('set_name', clientId, name); + }); + socket.on('set_color', function (clientId, color) { + self.trigger('set_color', clientId, color); + }); + socket.on('ack', function (revision) { + self.trigger('ack', revision); + }); + socket.on('operation', function (clientId, revision, operation, selection) { + self.trigger('operation', revision, operation); + self.trigger('selection', clientId, selection); + }); + socket.on('operations', function (head, operations) { + operations = LZString.decompressFromUTF16(operations); + operations = JSON.parse(operations); + self.trigger('operations', head, operations); + }); + socket.on('selection', function (clientId, selection) { + self.trigger('selection', clientId, selection); + }); + socket.on('reconnect', function () { + self.trigger('reconnect'); + }); + } + + SocketIOAdapter.prototype.sendOperation = function (revision, operation, selection) { + operation = LZString.compressToUTF16(JSON.stringify(operation)); + this.socket.emit('operation', revision, operation, selection); + }; + + SocketIOAdapter.prototype.sendSelection = function (selection) { + this.socket.emit('selection', selection); + }; + + SocketIOAdapter.prototype.getOperations = function (base, head) { + this.socket.emit('get_operations', base, head); + }; + + SocketIOAdapter.prototype.registerCallbacks = function (cb) { + this.callbacks = cb; + }; + + SocketIOAdapter.prototype.trigger = function (event) { + var args = Array.prototype.slice.call(arguments, 1); + var action = this.callbacks && this.callbacks[event]; + if (action) { + action.apply(this, args); + } + }; + + return SocketIOAdapter; + +}());
\ No newline at end of file |