2016-12-01 59 views
0

我在一个node.js服务器上测试了IBMIoTF,它运行良好。 IBMIoTF你可以在这里找到:https://www.npmjs.com/package/ibmiotf如何在WebApplication中使用IBMIoTF for node.js?

现在我想用IBMIoTF在web应用和我注意到在文件中这个小注:https://www.npmjs.com/package/ibmiotf#load-the-library-in-browser

加载在浏览器 负载IOTF客户端库-bundle.js或dist目录中的iotf-client-bundle-min.js

我也看了看http://browserify.org/,但我无法让它工作。

它能够加载库在的index.html

<script src="libs/iotf/iotf-client-bundle.min.js"></script> 

,但我怎么能角模块中创建一个对象实例?

选项1

我不能够使用需要web应用。

var config = { 
         "org": "THEORG", 
         "id": "IOT_WEB_APPLICATION", 
         "auth-key": "THEKEY", 
         "auth-token": "THETOKEN", 
         "type" : "shared" 
       }; 

var IotClient = require('ibmiotf'); 
var iotClient = new IotClient.IotfApplication(config); 

在这种情况下,我得到

angular.js:14110 ReferenceError: require is not defined 

选项2

我还试图用一个对象,我在IOTF-client.js文件中找到。

module.exports = { 
    IotfDevice: _IotfDevice['default'], 
    IotfManagedDevice: _IotfManagedDevice['default'], 
    IotfGateway: _IotfGateway['default'], 
    IotfManagedGateway: _IotfManagedGateway['default'], 
    IotfApplication: _IotfApplication['default'] 
    }; 

并没有在我的控制器这样的实现:

var config = { 
       "org": "THEORG", 
       "id": "IOT_WEB_APPLICATION", 
       "auth-key": "THEKEY", 
       "auth-token": "THETOKEN", 
       "type" : "shared" 
      }; 
var iotClient = new IotfApplication(config); 

在这里,我得到:

angular.js:14110 ReferenceError: IotfApplication is not defined 

这些选项没有工作,但如何创建一个的实例IBMIoTF? 任何人都可以帮助我吗?

回答

1

您需要browserify的ibmiotf为您buildprocess的一部分:
在你的package.json 1.添加依赖于ibmiotf NPM
2.做npm install
3.添加脚本命令添加到您的package.json为browserify /丑化这样

"scripts": { 
"build": "browserify your.js | uglifyjs -m -c warnings=false > bundle.js" 
} 
  • npm build,这将产生具有所有JavaScript文件一个bundle.js和指定的依赖关系bundle.js

  • 将bundle.js包含在您的web html文件中。 ...<script src="bundle.js"></script>

  • 在“你的。JS”做这样的事情

    var config = require(YOURCONFIG); var deviceType = "YOURDEVICETYPE"; var appClient = new client.IotfApplication(config); appClient.connect(); appClient.on("connect", function() { console.log("Connected"); appClient.subscribeToDeviceEvents(deviceType); }); appClient.on("deviceEvent", function (deviceType, deviceId, eventType, format, payload) { console.log("Device Event from :: "+deviceType+" : "+deviceId+" of event "+eventType+" with payload : "+payload); });

  • +0

    刘若英您好,感谢您的反馈意见。我做了一个基本的样本这里https://github.com/thomassuedbroecker/browserfied-ibmiotf-webapplication的斜网应用程序。关心托马斯 –