首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用ShareJS的跨域请求

使用ShareJS的跨域请求
EN

Stack Overflow用户
提问于 2016-09-11 23:01:51
回答 1查看 134关注 0票数 0

我正在尝试为开发一个DokuWiki插件,它将支持对wiki页面的实时协同编辑。我正在使用Node.jsShareJS,但我遇到了一些麻烦,因为这是我第一次使用它们.

到目前为止,我得到的是基于页面的。

ShareJS服务器- http://localhost:3000

代码语言:javascript
复制
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);

顺便说一句,当我运行它时,它会输出这个警告:

DokuWiki

在编辑wiki页面时,比如http://localhost/dokuwiki/doku.php?id=start&do=edit,插件包括以下脚本:

  • http://localhost:3000/channel/bcsocket.js
  • http://localhost:3000/share/share.js
  • http://localhost:3000/share/textarea.js

然后执行以下操作:

代码语言:javascript
复制
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页面编辑器出现以下错误:

我遗漏了什么?提前感谢!

EN

回答 1

Stack Overflow用户

发布于 2016-09-12 22:11:40

) DeprecationWarning仍然存在,我不知道为什么,但它现在正在工作。

只需编辑ShareJS Server代码:

  • browserChannel: { cors: "http://localhost/dokuwiki/" },选项中删除ShareJS行
  • 使用Access-Control-Allow-Origin中间件进行调整

下面是“最终”ShareJS Server代码:

代码语言:javascript
复制
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文本区域。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39441474

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档