有人用过bone.io这个Nodejs框架吗?

有人用过bone.io这个Nodejs框架吗?

觉是这个双向通信的设计思路挺好的,但是照着 http://bone.io/ 上面的的文档写了个例子,不知道如何把 route和 inbound/outbound 关联起来 在设置 inbound/outbound的时候 带命名空间,

  bone.io('chat', {
    outbound: {
        routes: ['broadcast']
    },
    inbound: {
        register: function(data, context) {
            this.join(data.room);
        },
        deregister: function(data, context) {
            this.leave(data.room);
        },
        send: function(data, context) {
            this.room(data.room).broadcast({message: data.message});
        }
    }
});

但是调置router的时候,又没有命名空间

bone.router({
    routes: {
        "start":"hello",
        "find/:query/p:page":   "search"   // #find/bones/p7
    },
    hello: function(context) {
        console.log(context);  
    },
    find: function(query, page) {
 console.log('find');  
       console.log(query);
 
    }
});

不知道两段代码中的routes 是根据什么关联起来的


3 回复

题目: 有人用过bone.io这个Nodejs框架吗?

内容:

骨朵社区的小伙伴们,我最近在研究一个名为 bone.io 的 Node.js 框架。它设计了一个双向通信机制,看起来非常有趣。但我遇到了一些问题,在使用 bone.io 进行开发时,对于如何将 routeinbound/outbound 关联起来感到困惑。

示例代码

首先,让我们回顾一下你提供的两个代码片段。第一个代码片段定义了 bone.ioinboundoutbound 通道:

bone.io('chat', {
    outbound: {
        routes: ['broadcast']
    },
    inbound: {
        register: function(data, context) {
            this.join(data.room);
        },
        deregister: function(data, context) {
            this.leave(data.room);
        },
        send: function(data, context) {
            this.room(data.room).broadcast({message: data.message});
        }
    }
});

在这个例子中,outbound.routes 被设置为 ['broadcast'],这意味着当数据通过 outbound 发送时,会触发 broadcast 动作。

接下来,我们看看 bone.router 的配置:

bone.router({
    routes: {
        "start":"hello",
        "find/:query/p:page": "search"   // #find/bones/p7
    },
    hello: function(context) {
        console.log(context);  
    },
    search: function(query, page) {
        console.log('find');  
        console.log(query);
    }
});

这里定义了两个路由:startfind/:query/p:page。当请求匹配这些路由时,相应的处理器函数(hellosearch)会被调用。

如何关联

现在,问题是如何将这两个部分关联起来。从 bone.io 的官方文档来看,bone.iobone.router 可能是两个独立的模块。因此,你需要明确地在代码中将它们连接起来。以下是一个可能的解决方案:

// 定义 bone.io
bone.io('chat', {
    outbound: {
        routes: ['broadcast']
    },
    inbound: {
        register: function(data, context) {
            this.join(data.room);
        },
        deregister: function(data, context) {
            this.leave(data.room);
        },
        send: function(data, context) {
            this.room(data.room).broadcast({message: data.message});
        }
    }
});

// 定义 bone.router
bone.router({
    routes: {
        "start":"hello",
        "find/:query/p:page": "search"
    },
    hello: function(context) {
        console.log(context);  
    },
    search: function(query, page) {
        console.log('find');  
        console.log(query);
    }
});

// 将 bone.io 和 bone.router 连接起来
bone.io.on('broadcast', function(message) {
    // 在这里你可以调用 bone.router 中定义的路由
    bone.router.handle('start', message);
});

在这个例子中,我们假设 bone.iobroadcast 动作可以触发一个事件,然后我们可以使用 bone.routerhandle 方法来处理这个消息,并调用相应的路由处理器。

希望这能帮助你理解如何将 bone.iobone.router 关联起来。如果还有其他问题,欢迎继续讨论!


是不是太新了?没找到栗子

bone.io 是一个用于实现客户端和服务端双向通信的 Node.js 框架。根据你的描述,你希望将 inboundoutbound 中定义的路由与 bone.router 中定义的路由关联起来。bone.io 的设计初衷是通过事件来处理这些通信。

bone.io 中,inboundoutbound 的路由与 bone.router 的路由并不直接关联,而是通过特定的事件来触发相应的逻辑。你可以通过事件名来匹配 bone.ioinboundoutbound 路由与 bone.router 的路由。

下面是一个示例代码,展示如何将 bone.ioinboundoutboundbone.router 结合使用:

const bone = require('bone.io');
const server = bone.listen(3000);

// 定义 inbound 和 outbound 路由
bone.io('chat', {
    outbound: {
        routes: ['broadcast']
    },
    inbound: {
        register: function(data, context) {
            this.join(data.room);
        },
        deregister: function(data, context) {
            this.leave(data.room);
        },
        send: function(data, context) {
            this.room(data.room).broadcast({ message: data.message });
        }
    }
});

// 定义 bone.router 路由
bone.router({
    routes: {
        "start": "hello",
        "find/:query/p:page": "search" // #find/bones/p7
    },
    hello: function(context) {
        console.log(context);
    },
    find: function(query, page) {
        console.log('find');
        console.log(query);
    }
});

// 通过事件名将两者关联起来
server.on('connection', (socket) => {
    socket.on('broadcast', (data) => {
        // 这里可以调用骨架构造函数或任何你需要的处理逻辑
        console.log(`Broadcasting message: ${data.message}`);
    });
});

在这个示例中,我们首先创建了一个 bone.io 实例并定义了 inboundoutbound 路由。然后,我们定义了 bone.router 的路由,并通过事件名 broadcast 将两者关联起来。当客户端发送一个 broadcast 事件时,服务端会捕获该事件并进行相应的处理。这样就实现了 bone.ioinboundoutbound 路由与 bone.router 路由之间的关联。

回到顶部