Azure WebSocket 不适用于 Go httpPlatformHandler

Azure WebSocket not working with Go httpPlatformHandler

我很难让 websocket 在 Azure Web 应用程序上运行,我目前正在将 Node 应用程序从 Azure 移植到 Go 应用程序。

门户网站上启用了 WebSocket,这是我的 web.config

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.web>
        <customErrors mode="Off"/>
    </system.web>
    <system.webServer>
        <webSocket enabled="false" />
        <handlers>
            <add name="httpplatformhandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" />
        </handlers>
        <httpPlatform stdoutLogEnabled="true" processPath="d:\home\site\wwwroot\v-0.0.10.exe"
                      arguments=""
                      startupTimeLimit="60">
            <environmentVariables>
              <environmentVariable name="GOROOT" value="d:\home\site\wwwroot\go" />
            </environmentVariables>
        </httpPlatform>
    </system.webServer>
</configuration>

当然在本地它工作正常。起初我只试过这个样本:

package main

import (
    "code.google.com/p/go.net/websocket"
    "io"
    "net/http"
    "os"
)

func echoHandler(ws *websocket.Conn) {
    io.Copy(ws, ws)
}

func main() {
    http.Handle("/echo", websocket.Handler(echoHandler))
    http.Handle("/", http.FileServer(http.Dir(".")))
    err := http.ListenAndServe(":"+os.Getevn("HTTP_PLATFORM_PORT"), nil)
    if err != nil {
        panic("ListenAndServe: " + err.Error())
    }
}

在客户端,我只是尝试连接:

var ws = new WebSocket("url")
ws.onopen = function() { ws.send("test"); }
ws.onmessage = function(e) { console.log(e.data); }

在Azure上部署时,readystate始终为0,onopen永远不会调用。它最终失败了,说连接出错:

WebSocket connection to 'wss://***.azurewebsites.net/echo' failed: Error during WebSocket handshake: Unexpected response code: 500

我什至尝试使用 Gorilla Websocket 示例来查看 Go 扩展包是否有问题。

我尝试过的事情:

  1. 从 web.config 中删除了 <webSocket enabled="false" /> [同样的问题,但似乎需要更长时间才能发生

  2. 添加标准 http.HandleFunc 来服务 "standard" 页面,它工作正常。

我有一些 Go 应用程序部署到 Azure,这只是我需要使用 websocket 的第一个,我想知道我做错了什么/不理解。

如有任何帮助,我们将不胜感激

我参考了您的代码并创建了一个示例,它对我有用。 我没有做任何自定义 web.config 但使用来自 Azure 部署脚本的默认值。

这是示例存储库:https://github.com/shrimpy/gowebsockettry