2017-05-28 42 views
1

我还没有收到任何事件通知,并想知道我是否缺少一些东西。 我遵循Fabric Composer网站的指示,在我的cto模型中定义了BasicEvent,并添加了用于在事务中创建和发布事件并更新网络的代码。我创建了一个单独的eventListener.js程序,该程序使用来自网站的代码示例使用businessNetworkConnection订阅事件。
当我启动我的eventListener.js应用程序后,它似乎在监听(在控制台收到连接的状态消息后,没有其他事情发生......它不会回到正常提示行)。 然后执行事务应该发出事件并且它成功运行,但在运行eventlistener.js的其他终端窗口中没有收到事件。
这里是eventListener.js计划的关键部分:如何监听Hyperledger Fabric Composer中的事件?

businessNetworkConnection.connect(connectionProfile, businessNetworkIdentifier, participantId, participantPwd) 
 
.then((result) => { 
 
    businessNetworkDefinition = result; 
 
    console.log('Connected: BusinessNetworkDefinition obtained=' + businessNetworkDefinition.getIdentifier()); 
 
    }); 
 
businessNetworkConnection.on('event', (event) => { 
 
    // event: { "$class": "org.namespace.BasicEvent", "eventId": "0000-0000-0000-000000#0" } 
 
    console.log(event); 
 
    });

是在businessNetworkConnection.on( '事件',(事件)...命令应该引起程序出现而其听挂?
如果是这样,有没有别的东西可以做,以解决那里的问题是什么?
我使用的是当地码头工人V0.6 HLF。

+0

它不应该导致它挂起。你可以试试'DEBUG = composer:* node myApplication.js'来获得一些调试信息。 –

回答

0

我认为你的问题是,在注册侦听器之前,你并没有等待businessNetworkDefinition被连接。请记住,then块是异步执行的,并且您的以下businessNetworkConnection.on代码将立即执行。

你应该添加第二个then块注册侦听后businessNetworkDefinition已连接。

例如

return adminConnection.connect('hlfv1', 'admin', 'adminpw') 
    .then(() => { 
     return BusinessNetworkDefinition.fromDirectory(path.resolve(__dirname, '..')); 
    }) 
    .then((businessNetworkDefinition) => { 
     return adminConnection.deploy(businessNetworkDefinition); 
     // return true; 
    }) 
    .then(() => { 
     businessNetworkConnection = new BusinessNetworkConnection(); 
     return businessNetworkConnection.connect('hlfv1', 'my-network', 'admin', 'adminpw'); 
    }) 
    .then(() => { 
     businessNetworkConnection.on('event', (event) => { 
      console.log('****** received the event ' + JSON.stringify(businessNetworkConnection.getBusinessNetwork().getSerializer().toJSON(event))); 
     }); 
    }); 
0

我为Hyperledger团队开发的基本示例应用程序编写了示例代码。你可以从这个代码片段看看:SampleEventListener