AWS 最近推出了具有套接字功能的 API 网关,并且可以代理到 Lambda。据我观察,AWS API 套接字完全是 WSS 协议,因此我已经完成了自定义
我对套接字很陌生,评论中的代码显示了我的尝试失败。我还尝试过搜索,但在 AWS API 套接字和 Socket.io 方面找不到太多信息。因此,感谢任何指导或支持。
connect
设置,该设置在代码部分中提供。Lambda 和其他设置我已经学习过,并按照此链接中所述使用 wscat 进行了测试 -
https://hackernoon.com/websockets-api-gateway-9d4aca493d39
但是,在与浏览器中的 Socket.io 集成后,它会在18-19秒后持续重新连接,但连接从未建立!Lambda 和 API 网关超时时间为30秒,我认为这可能可以忽略,因为它不会在4-5分钟内打断与 wscat 的连接。我对套接字很陌生,评论中的代码显示了我的尝试失败。我还尝试过搜索,但在 AWS API 套接字和 Socket.io 方面找不到太多信息。因此,感谢任何指导或支持。
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.3.0/socket.io.js"></script>
</head>
<script>
socket = io(
`wss://abcd.execute-api.us-east-1.amazonaws.com/prod`
, {
path: `/prod/`,
forceNew: true,
// autoConnect: false,
// reconnection: false,
upgrade: true,
transports: ['websocket',
'polling',
'flashsocket'
]
});
socket.on('connect', function () {
socket.emit({"action":'message',"data":"hello return"}, () => {
console.log("message Sent");
});
});
// socket.emit({"action":'message',"data":"hello return"}, () => {
// console.log("message"); // true
// });
// socket.on('message', () => {
// console.log("message"); // true
// });
// socket.on("connection", function (socket) {
// console.log("connection")
// })
// socket.on('connect', () => {
// console.log("connect"); // true
// });
// socket.on('connected', () => {
// console.log("connected"); // true
// });
// socket.on('open', () => {
// console.log("open"); // true
// });
// socket.on('disconnect', (reason) => {
// console.log("disconnect->",reason);
// // if (reason === 'io server disconnect') {
// // // the disconnection was initiated by the server, you need to reconnect manually
// // socket.connect();
// // }
// // else the socket will automatically try to reconnect
// });
// socket.on('close', (reason) => {
// console.log("close->",reason);
// // if (reason === 'io server disconnect') {
// // // the disconnection was initiated by the server, you need to reconnect manually
// // socket.connect();
// // }
// // else the socket will automatically try to reconnect
// });
// // socket.open();
// // console.log(socket);
// socket.on( (res) => {
// console.log("res", res);
// });
// socket.on('reconnect', (attemptNumber) => {
// console.log("reconnected", attemptNumber); // true
// });
// socket.on('reconnecting', (attemptNumber) => {
// console.log("reconnecting...", attemptNumber); // true
// });
// socket.on('reconnect_error', (error) => {
// console.log("reconnect_error",error);
// });
// socket.on('reconnect_attempt', (error) => {
// console.log("reconnect_attempt->", error)
// // socket.io.opts.transports = ['polling', 'websocket'];
// });
// socket.on('error', (error) => {
// console.log("error->", error);
// });
// socket.on('ping', () => {
// console.log("ping");
// });
// socket.on('pong', () => {
// console.log("ping");
// });
</script>
</html>