我正在尝试为DokuWiki开发一个插件,以便实现wiki页面的实时协作编辑。我正在使用Node.js和ShareJS这样做,但我遇到了一些麻烦,因为这是我第一次使用它们...
var express = require('express');
var app = express();
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', 'http://localhost:3000');
res.header('Access-Control-Allow-Methods', 'GET, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
res.header('Access-Control-Allow-Credentials', true);
return next();
});
// public folder to store assets
app.use(express.static(__dirname + '/public'));
// get sharejs dependencies
var sharejs = require('share');
require('redis');
// options for sharejs
var options = {
db: {type: 'redis'},
browserChannel: { cors: "http://localhost/dokuwiki/" },
};
// attach the express server to sharejs
sharejs.server.attach(app, options);
// listen on port 3000 (for localhost) or the port defined for heroku
var port = process.env.PORT || 3000;
app.listen(port);
顺便说一下,当我运行它时会输出此警告:
编辑维基页面时,请说http://localhost/dokuwiki/doku.php?id=start&do=edit
,插件包含以下脚本:
然后执行:
window.onload = function() {
// get dokuwiki editor textarea element
var pad = document.getElementById('wiki__text');
if (pad) { // if a wiki page is being edited
// Server options
var options = {
origin: "http://localhost:3000/channel"
};
// Connect to the server
var connection = sharejs.open('test', 'text', options, function(error, doc) {
doc.attach_textarea(pad);
});
}
};
导致DokuWiki页面编辑器出现以下错误:
我错过了什么?提前谢谢!
答案 0 :(得分:0)
管理解决它! :) DeprecationWarning 仍在那里,我不知道为什么,但它现在正在运作。
只需编辑 ShareJS服务器代码:
从ShareJS选项
browserChannel: { cors: "http://localhost/dokuwiki/" },
行
使用 Access-Control-Allow-Origin 中间件
以下是“最终” ShareJS服务器代码:
var express = require('express');
var app = express();
var port = 3000;
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With, content-type');
res.setHeader('Access-Control-Allow-Credentials', true);
next();
});
// public folder to store assets
app.use(express.static(__dirname + '/public'));
// get sharejs dependencies
var sharejs = require('share');
require('redis');
// options for sharejs
var options = {
db: {type: 'redis'},
};
// attach the express server to sharejs
sharejs.server.attach(app, options);
app.listen(port, function() {
console.log('listening on *:' + port);
});
就是这样!现在我只需要编辑这个代码以使不同的wiki页面具有不同的ShareJS textareas。