如何在Node.js中的Azure部署之前配置WebSocket

问题描述

我正在尝试在node.js中配置websocket并在azure应用程序服务上进行部署,并且我将Flutter应用程序用作客户端。

在服务器端(Node.js)

const https = require('https');
const WebSocket = require('ws');
const express = require("express");
const app = express();
const server = https.createServer(app);

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

wss.on('connection',function connection(ws) {
ws.on('message',function incoming(message) {
console.log(message);
});

ws.send('something');
});

在客户端(颤振)

 channel = IOWebSocketChannel.connect("wss://{name}.azurewebsites.net");
  channel.stream.listen((message) {
    print(message);
   channel.sink.add("received!");

});

客户端错误

Unhandled Exception: WebSocketChannelException: WebSocketChannelException: WebSocketException: 
Connection to 'http://{name}.azurewebsites.net:0#' was not upgraded to websocket

注意

    @H_502_35@借助websocket.Server()配置中的{port:8080},我能够成功连接本地主机中的客户端和服务器 @H_502_35@这些代码也在heroku服务器上进行了测试,但错误无法修复。 @H_502_35@我还启用了Azure云上的Websocket配置

解决方法

根据这篇文章:SignalR Core not using Websockets in Azure App Service

您应该有一个在Azure上启用WebSocket的选项