Azure WebSocket 无法与 Go httpPlatformHandler 一起使用

标签 azure go websocket azure-web-app-service

我很难让 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. 删除了 <webSocket enabled="false" />来自 web.config [同样的问题,但似乎需要更长的时间才会发生

  2. 添加标准 http.HandleFunc提供“标准”页面并且工作正常。

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

任何帮助将不胜感激

最佳答案

我引用了您的代码并创建了一个示例,它对我有用。 我没有执行任何自定义 web.config,而是使用来自 Azure 部署脚本的默认 web.config。

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

enter image description here

关于Azure WebSocket 无法与 Go httpPlatformHandler 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33675348/

相关文章:

python - 使用 python 在 azure 上将基础设施作为代码

go - go/kubernetes-runtime.notRegisteredErr

go - Go 语言中的引用类型令人困惑

node.js - Socket.io 不同的客户端 ID

azure - 如何从 Eventhub 中删除事件

azure - 如何删除混合连接

c++ - C++中的websocket fin位

javascript - ng-websocket 和 ws : client sends successfully but doesn't receive

azure - 与每个订阅的多个自动化帐户相关的架构决策

go - smtp.Dial ("ASPMX.L.GOOGLE.COM:25") 连接错误;但是腻子连接有效