Created
August 14, 2018 13:33
-
-
Save hata6502/850b67ccaad2f5714f4f4b198cac6858 to your computer and use it in GitHub Desktop.
npm でインストールした ot.js の EditorSocketIOServer が動かない問題 ref: https://qiita.com/hato/items/8ad93c0fde62a145d6db
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var EditorSocketIOServer = require('ot/lib/editor-socketio-server'); | |
/** | |
* ot.js patch for socket.io >= 1.0 | |
* Copyright © 2012-2014 Tim Baumann, http://timbaumann.info | |
* Released under the MIT License, https://github.com/Operational-Transformation/ot.js/blob/master/LICENSE | |
*/ | |
{ | |
var EventEmitter = require('events').EventEmitter; | |
var TextOperation = require('ot/lib/text-operation'); | |
var WrappedOperation = require('ot/lib/wrapped-operation'); | |
var Server = require('ot/lib/server'); | |
var Selection = require('ot/lib/selection'); | |
var util = require('util'); | |
EditorSocketIOServer.prototype.addClient = function (socket) { | |
var self = this; | |
socket | |
.join(this.docId) | |
.emit('doc', { | |
str: this.document, | |
revision: this.operations.length, | |
clients: this.users | |
}) | |
.on('operation', function (revision, operation, selection) { | |
self.mayWrite(socket, function (mayWrite) { | |
if (!mayWrite) { | |
console.log("User doesn't have the right to edit."); | |
return; | |
} | |
self.onOperation(socket, revision, operation, selection); | |
}); | |
}) | |
.on('selection', function (obj) { | |
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)); | |
}); | |
}) | |
.on('disconnect', function () { | |
console.log("Disconnect"); | |
socket.leave(self.docId); | |
self.onDisconnect(socket); | |
if ( | |
(socket.manager && socket.manager.sockets.clients(self.docId).length === 0) || // socket.io <= 0.9 | |
(socket.ns && Object.keys(socket.ns.connected).length === 0) // socket.io >= 1.0 | |
) { | |
self.emit('empty-room'); | |
} | |
}); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment