NodeJS中的Faye Websocket

时间:2016-08-09 07:20:50

标签: javascript node.js websocket faye webix

我是nodeJS和faye / websockets的新手。

使用webix制作数据表,我想对其进行实时更新。

现在正试图与Faye合作,但不太确定在faye向我发送看起来像这样的请求之后该怎么做:

[{"channel":"/meta/handshake","version":"1.0","supportedConnectionTypes":["websocket","eventsource","long-polling","cross-origin-long-polling","callback-polling"],"id":"1"}]

并得到答复:

[{"id":"1","channel":"/meta/handshake","successful":true,"version":"1.0","supportedConnectionTypes":["long-polling","cross-origin-long-polling","callback-polling","websocket","eventsource","in-process"],"clientId":"8bkeysctea7ucerz9h3tyjz0n52s5os","advice":{"reconnect":"retry","interval":0,"timeout":45000}}]

在服务器(NodeJS)上,我有以下代码:

var http = require("http"),
    faye = require("faye");


    var server = http.createServer(function (request, response) {
           //Here i have some code for the CRUD operations for the webix Datatable    
    }),
       bayeux = new faye.NodeAdapter({ mount: '/faye', timeout: 45 });

    bayeux.attach(server);
    server.listen(1212, function () {
       console.log("\r\n" + new Date() + "\n -->> HTTP Server listens to Port 1212 <<--\r\n ");
    });

在客户端(JS):

webix.proxy.faye.client = new Faye.Client("//localhost:1212/faye");    
webix.proxy.faye.clientId = webix.uid(); // unique client id
webix.proxy.faye.client.connect();

HTML:

<script type="text/javascript" src="//localhost:1212/faye/client.js"></script>

(从webix示例+ faye示例中获得了很多东西)

也许有人可以解释我下一步该怎么做,或者提供一个链接,我可以阅读更多关于Websockets以及如何使用它们的信息。

感谢您的时间。

1 个答案:

答案 0 :(得分:0)

在客户端和服务器端配置faye后,可以将其用作任何组件的数据源

{
view: "datatable", 
url: "faye->/data", 
save: "faye->/data"
}

请注意,faye仅用于在客户端之间同步数据,它不会将数据加载或保存到真实数据库