我编写了这个超级简单的客户端,以通过websocket协议(javascript版本)向aws sdk发布消息。 https://github.com/aws/aws-iot-device-sdk-js
var awsIot = require('aws-iot-device-sdk');
var device = awsIot.device({
region: "us-west-2",
protocol: "wss",
clientId: "ARUNAVS SUPER TEST",
host: "iot.us-west-2.amazonaws.com",
port: "443"
});
device
.on('connect', function() {
console.log('connect');
device.publish('abcd', JSON.stringify({ test_data: 1}));
});
device
.on('message', function(topic, payload) {
console.log('message', topic, payload.toString());
});
device
.on('error', function(error) {
console.log('error', error);
});
我收到以下错误(在导入管理员凭证https://github.com/aws/aws-iot-device-sdk-js#websockets):-
之后node testCode.js
error { Error: unexpected server response (403)
at ClientRequest._req.on
(/Users/arunavs/mrtests/node_modules/ws/lib/WebSocket.js:653:21)
at emitOne (events.js:96:13)
at ClientRequest.emit (events.js:188:7)
at HTTPParser.parserOnIncomingClient (_http_client.js:472:21)
at HTTPParser.parserOnHeadersComplete (_http_common.js:105:23)
at TLSSocket.socketOnData (_http_client.js:361:20)
at emitOne (events.js:96:13)
at TLSSocket.emit (events.js:188:7)
at readableAddChunk (_stream_readable.js:177:18)
at TLSSocket.Readable.push (_stream_readable.js:135:10)
type: 'error',
target:
WebSocket {
domain: null,
_events: {},
_eventsCount: 0,
_maxListeners: undefined,
readyState: 3,
bytesReceived: 0,
extensions: null,
protocol: '',
_binaryType: 'arraybuffer',
_finalize: [Function: bound finalize],
_closeFrameReceived: false,
_closeFrameSent: false,
_closeMessage: '',
_closeTimer: null,
_finalized: true,
SDK无法给出我得到403的任何原因。
注意:根据https://github.com/aws/aws-iot-device-sdk-js/blob/234d170c865586f4e49e4b0946100d93f367ee8f/device/index.js#L142,该代码甚至使用sigv4进行了预签名,因为我的输出中也包含
url: 'wss://iot.us-west-2.amazonaws.com:443/mqtt?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential= .......
有人看到这样的错误吗?
答案 0 :(得分:0)
我认为,您发布的是THING,不允许所有用户与其连接。
您可以张贴您要在其上发布消息的事物的策略的详细信息吗。
在“创建策略”页面上的“名称”字段中,为 策略(例如,MyIoTButtonPolicy)。在“操作”字段中,键入 物联网:连接。在“资源ARN”字段中,键入*。选择允许 复选框。这样,所有客户端都可以连接到AWS IoT。
详细了解POLICIES。
PS:这只是一个疯狂的猜测。请在问题中发布政策详细信息,以便我确定。