LoginSignup
1
0

More than 5 years have passed since last update.

npm でインストールした ot.js の EditorSocketIOServer が動かない問題

Posted at

npm でインストールした ot.js の EditorSocketIOServer が動かない。
OT サーバーにクライアントがアクセスして、切断したときにエラー落ちしてしまう問題。

原因と回避策

npm でインストールした ot.js は、socket.io の最新版に対応していない。
しかし、GitHub では EditorSocketIOServer.prototype.addClient() が修正されているので、パッチをあてよう。

patch.js
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');
                }
            });
    };
}

Windows から OT サーバーを建てられるようにしました!

実は Node.js を使ったのは初めてだったのですが、想像以上に便利だったので C# で GUI 作りました。localot GUI

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0