Web 消息总线 JetBus
Jet 是一个用于 Web 的轻量级和实时的消息总线。支持浏览器和 Node.js node-jet,提供 Lua 版本 lua-jet 和基于 Arduino 的版本 Arduino-Jet。
示例代码:
varjet=require('node-jet');varpeer=newjet.Peer({
url:'ws://jet.nodejitsu.com:80'
});peer.connect().then(function(){
console.log('peerisconnectedtodaemon');
});varmachineName=newjet.State('machine/name','animal');
machineName.on('set',function(newName){
setMachineName(newName);//doessomethingappropriate
});peer.add(machineName).then(function(){
console.log('machine/namehasbeenaddedtodaemon');
}).catch(function(err){
console.log('couldnotaddmachine/nametodaemon',err);
});//addastate
varcpuLoad=newjet.State('cpu/load',readCpuLoad());peer.add(cpuLoad).then(function(){
//asyncpostnewvalue
setTimeout(function(){
cpuLoad.value(readCpuLoad());
},3000);
});
添加方法:
//addamethod vargreet=newjet.Method('greet'); greet.on('call',function(name){ if(name.first==='John'){ throw'Johnisabadguy!'; } vargreeting='Hello'+name.first+''+name.last; console.log(greeting); returngreeting; });peer.add(greet);