我在node.js服务器中测试了IBMIoTF,它运行良好。您可以在这里找到IBMIoTF:https://www.npmjs.com/package/ibmiotf
现在,我想在Web应用程序中使用IBMIoTF,并且在文档中注意到了这一小注释:https ://www.npmjs.com/package/ibmiotf#load-the-library-in-browser
在浏览器中加载库从dist目录加载iotf-client-bundle.js或iotf-client-bundle-min.js
我还查看了http://browserify.org/,但无法使其正常运行。
能够将库加载到index.html中
<script src="libs/iotf/iotf-client-bundle.min.js"></script>
,但是如何在angular模块中创建对象实例?
选项1
我无法在WebApplication中使用require。
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创建实例?谁能帮我?
您需要在构建过程中对ibmiotf进行浏览器化:
1.在package.json中将依赖项添加到ibmiotf npm
2.做npm install
3.将脚本命令添加到package.json中,以实现browserify / uglify像这样
"scripts": {
"build": "browserify your.js | uglifyjs -m -c warnings=false > bundle.js"
}
do npm build
,这将产生一个bundle.js,其中包含您所有的javascript文件以及指定给bundle.js的依赖项
在您的Web html文件中包含bundle.js。 ...<script src="bundle.js"></script>
在“ your.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); });
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句