这是我的代码。
// ========================================================================
// CONFIGURATION
// ========================================================================
var my_account_id='pxxxxxxxxxxtrial';
var my_device_id='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
var my_oauth_token='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
// ========================================================================
var endpoint = 'wss://iotmmspxxxxxxxxxxtrial.hanatrial.ondemand.com/com.sap.iotservices.mms/v1/api/ws/mqtt';
var options = {
keepalive: 60,
username: my_device_id,
password: my_oauth_token,
connectTimeout: 5 * 1000,
clientId: my_device_id,
protocolId: 'MQIsdp',
protocolVersion: 3,
clean: true,
will: null
};
console.log(options);
// ========================================================================
var mqtt = require('mqtt');
var client = mqtt.connect(endpoint, options);
client.on('connect', function () {
console.log('connected!');
var data = {"mode":"async","messageType":"919762ab861ea779534a","messages":[{"Weight": 99,"timestamp":1413191650}]};
client.publish('iot/data/'+my_device_id, JSON.stringify(data));
console.log(JSON.stringify(data));
});
client.on('error', function () {
console.log('error!');
});
client.on('offline', function () {
console.log('offline!');
});
client.on('close', function () {
console.log('close!');
});
client.on('message', function (topic, message) {
// message is Buffer
console.log(message.toString());
//client.end();
});
Console.log 输出仅显示“离线!” 和“关闭!”。它从不显示“已连接!”。我已经在此页面https://archive.sap.com/discussions/thread/3955899上尝试了答案,但我仍然无法使其工作。我的代码出了什么问题?