首页 > 解决方案 > Azure 中托管的节点服务器上的 Websocket 失败

问题描述

我有一个准系统的简单示例,可以在本地工作,但不能在 Azure 中工作。我最初的问题有点特定于一个 cors-error,我现在已经解决了这个问题,因此编辑掉了那部分。但我仍然无法让 WS 在 azure 上工作。我需要帮助。

var server = http.Server(app);
const wss = new WebSocket.Server({server});

server.listen(8070, () => {
  console.log("Listening on " + port)
});


wss.on('connection', (socket) => {
  console.log('a user connected');
  socket.on('message', (msg) => {
    console.log(msg)
    socket.send(JSON.stringify({hey:'you'}))
  })
});

客户端连接字符串:

SOCKET_ENDPOINT = 'wss://******.azurewebsites.net/';

我在我的应用程序配置中打开了 websockets。

编辑:

所以我现在已经转动了每一块石头。甚至将库从 socket.io 切换到 ws。同样的问题。我想在尝试不同的事情时走得更远,现在我收到以下错误“Firefox 无法在 wss://*******.azurewebsites.net:8070/ 建立与服务器的连接。”

Edit2:http-upgrade 很好 在此处输入图像描述

这是错误 在此处输入图像描述

编辑 3:卷曲命令:

* TCP_NODELAY set
* Connected to *******.azurewebsites.net (20.40.202.6) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* successfully set certificate verify locations:
*   CAfile: /etc/ssl/certs/ca-certificates.crt
  CApath: /etc/ssl/certs
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* TLSv1.2 (IN), TLS handshake, Certificate (11):
* TLSv1.2 (IN), TLS handshake, Server key exchange (12):
* TLSv1.2 (IN), TLS handshake, Server finished (14):
* TLSv1.2 (OUT), TLS handshake, Client key exchange (16):
* TLSv1.2 (OUT), TLS change cipher, Change cipher spec (1):
* TLSv1.2 (OUT), TLS handshake, Finished (20):
* TLSv1.2 (IN), TLS handshake, Finished (20):
* SSL connection using TLSv1.2 / ECDHE-RSA-AES256-GCM-SHA384
* ALPN, server did not agree to a protocol
* Server certificate:
*  subject: CN=*.azurewebsites.net
*  start date: Sep 28 19:00:01 2020 GMT
*  expire date: Sep 28 19:00:01 2021 GMT
*  subjectAltName: host "********.azurewebsites.net" matched cert's "*.azurewebsites.net"
*  issuer: C=US; O=Microsoft Corporation; CN=Microsoft RSA TLS CA 01
*  SSL certificate verify ok.
> GET /socket.io/?EIO=4 HTTP/1.1
> Host: *********.azurewebsites.net
> User-Agent: curl/7.68.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 401 Unauthorized
< Server: Kestrel
< WWW-Authenticate: Bearer realm="********.azurewebsites.net" authorization_uri="https://login.windows.net/<tenant>/oauth2/authorize" resource_id="<resource>"
< Date: Tue, 22 Jun 2021 05:56:15 GMT
< Content-Length: 0
< 
* Connection #0 to host *******.azurewebsites.net left intact

标签: node.jsangularazuresocket.iows

解决方案


我猜您在Azure AppService中托管您的应用程序?

确保您在平台中打开了 websocket 选项: 在此处输入图像描述

AppService 资源 -> 设置 -> 常规设置选项卡 -> 平台设置部分


推荐阅读