我已经使用 WebSocket 协议设置了 AWS API 网关。使用以下代码,我能够连接。
import React, { useState, useEffect, useRef } from 'react';
import { Signer, Auth} from 'aws-amplify';
import Sockette from 'sockette';
export default function WebsocketSmaple(props) {
const [messages, setMessages] = useState([]);
const socketRef = useRef();
useEffect(() => {
async function connectWebsocket(){
const credentials = await Auth.currentCredentials();
const accessInfo = {
access_key: credentials.accessKeyId,
secret_key: credentials.secretAccessKey,
session_token: credentials.sessionToken,
}
let signedUrl = Signer.signUrl('wss://MY-API-ID.execute-api.REGION.amazonaws.com/dev', accessInfo)
socketRef.current = new Sockette(signedUrl , {
timeout: 5000,
maxAttempts: 1,
onopen: e => console.log('Connected!', e),
onmessage: e => {
console.log('Received:', e)
setMessages(prev => [...prev, e.data])
},
onreconnect: e => console.log('Reconnecting...', e),
onmaximum: e => console.log('Stop Attempting!', e),
onclose: e => console.log('Closed!', e),
onerror: e => console.log('Error:', e)
});
}
connectWebsocket()
return () => {
socketRef.current.close();
};
}, []);
...some codes
}
但是当我设置自定义域名并更改代码如下所示时,出现以下错误。
let signedUrl = Signer.signUrl('wss://MY-API-ID.execute-api.REGION.amazonaws.com/dev', accessInfo)
至
let signedUrl = Signer.signUrl('wss://MY-CUSTOM-DOMAIN-NAME/MAPPED-PATH', accessInfo)
错误
Uncaught (in promise) TypeError: Cannot read property 'byteLength' of undefined
at isEmptyData (jsSha256.ts:93)
at Sha256.update (jsSha256.ts:34)
at encrypt (Signer.ts:27)
at get_signing_key (Signer.ts:220)
at Function.Signer.signUrl (Signer.ts:413)
at websocket (WebsocketSmaple.js:40)
如何使用自定义域名和 AWS_IAM 授权方法进行连接?
谢谢你。