【发布时间】:2020-01-31 01:49:42
【问题描述】:
我最近在我们的测试环境中从 Azure 应用服务 Windows 切换到了 Linux。除了我们的套接字连接之外,一切都像以前一样工作。似乎有很多关于 Linux App Service 的过时信息,文档也乏善可陈。但是,根据these release notes 的说法,Azure App Service Linux 上的 Web 套接字支持。
在某些Azure App Service for Linux documentation 中,它声明您必须禁用perMessageDeflate 才能使Web Sockets 与Linux App Service 和NodeJS 一起使用。我相信我已经在下面的 HapiJS 服务器代码中做到了这一点。我用console.log(io) 验证了设置perMessageDeflate 似乎正确设置为false。
import Server from 'socket.io';
import socketioJwt from 'socketio-jwt';
const myHapiJSPlugin = {
name: 'myPluginName',
version: '2.0.0',
register: function (server, options) {
const io = new Server(server.listener, {
perMessageDeflate: false,
transports: ['websocket'],
origins: '*:*'
});
io.use(socketioJwt.authorize({
secret: JWT_SECRET_KEY,
handshake: true
}));
io.on('connection', socket => {
console.log(io);
// more code here
};
};
};
当我在使用网络客户端时打开 Chrome 控制台的网络页面时,我从服务器收到 101 响应代码。我console.log 从 socket.io 的客户端回调中连接/断开连接。尽管得到了服务器的确认(101 响应),但我可以看到它不断地连接/断开连接。连接状态在控制台中显示“已停止”。当回调触发时,我似乎订阅了一个特定的路由。
尽管在下面的配置中添加了perMessageDeflate 和origins 用于测试socket.io docs,但自从从Azure App Services Windows 切换后,我没有进行任何其他代码更改。我认为在握手或身份验证过程中出了点问题。
Status Code: 101 Switching Protocols
Access-Control-Allow-Origin: *
Connection: Upgrade
Date: Tue, 01 Oct 2019 18:04:57 GMT
Sec-WebSocket-Accept: <HASH>
Server: Kestrel
Upgrade: websocket
我还在客户端代码中添加了perMessageDeflate。没什么区别。
const client = new io(URL, {
query: 'token=' + jwt,
perMessageDeflate: false,
transports: ['websocket'],
upgrade: false
});
我还缺少什么?如何在 Azure 应用服务 Linux 上启用 Web 套接字?我检查了类似 Windows 的配置设置。似乎没有设置 - 因为默认情况下似乎启用了 Web 套接字。我已经在日志中验证了 Web 服务器不会不断重新启动导致连接/断开连接。
【问题讨论】:
标签: node.js azure socket.io azure-web-app-service hapi