【发布时间】:2018-02-09 13:37:14
【问题描述】:
我目前正在运行 JanusGraph (0.1.1),但在尝试通过 Websocket 连接时遇到了一些困难 - 请注意,Gremlin 控制台工作正常。
我使用 Nodejs 作为客户端,并且尝试了所有可用的模块。我可以确认我确实通过了 SSL 安全性(提供证书本身或使用 rejectUnauthorized 选项)。然后,根据客户端库,我能够测试连接确实是打开的,但似乎没有查询通过。我得到的唯一线索是'gremlin'模块响应错误401 - 使用与http端点相同的凭据(有效)。
有人能指导我通过正确的方式来实现这个 Websocket 连接吗?我附上了两个带有两个不同库的示例。
// With Gremlin module
const Gremlin = require("gremlin");
const client = Gremlin.createClient(<my-port>, '<my-url>', {
path: '/',
user: '<my-user>',
password: '<my-password>',
ssl: true,
rejectUnauthorized: false,
session: true,
});
client.execute('def graph=ConfiguredGraphFactory.open("test3")', { }, (err, results) => {
if (err) {
return console.error(err)
}
console.log(results);
});
// With Ws module
const WebSocket = require('ws');
var fs = require('fs');
const ws = new WebSocket('wss://<my-url>:<my-port>', {
ca: fs.readFileSync("./ssl.cert"), // required to get passed the leaf certificate error
extraHeaders: {
Authorization: 'Basic ' + new Buffer('<my-user>:<my-password>').toString('base64')
},
perMessageDeflate: false,
ssl: true,
json: true,
strictSSL: false,
// rejectUnauthorized: false, // does not work - needs the certificate
});
ws.onopen = function (event) {
console.log('SSL OK: ', event.target._sender._socket.authorized);
ws.send("{'gremlin': 'def graph=ConfiguredGraphFactory.open(\"test3\");graph.addVertex(label,\"some vertex\");graph.tx().commit();'}", function(res){
console.log(res);
ws.close();
});
};
【问题讨论】:
-
在我看来,您的网址最后错过了
/gremlin。 Gremlin 服务器的默认 URL 是:ws://localhost:8182/gremlin'。 -
感谢 Florian,这是一个很好的尝试,因为 Gremlin 模块实际上是硬编码此 /gremlin 路径 - 因此我将路径指定为“/”,因为我的服务器是这样设置的。
标签: websocket gremlin tinkerpop janusgraph