【问题标题】:Azure WebSocket not working with Go httpPlatformHandlerAzure WebSocket 不适用于 Go httpPlatformHandler
【发布时间】:2015-11-13 10:17:50
【问题描述】:

我很难让 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 中删除了 &lt;webSocket enabled="false" /&gt; [同样的问题,但似乎需要更长时间才能发生

  2. 添加一个标准的http.HandleFunc 来提供一个“标准”页面,它工作正常。

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

任何帮助将不胜感激

【问题讨论】:

  • 这可能无关,但code.google.com/p/go.net/websocket 现在是golang.org/x/net/websocket
  • 是的,但是直到 2016 年 1 月,我认为根据我的理解,这个 repo 仍然可以,并且在我发现 code.google.com 正在运行后,在我的代码中我也将其更改为 golan.org/x/net/websoket停产这只是我从 Github 复制粘贴以解决 SO 问题;)

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


【解决方案1】:

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

这里是示例代码库:https://github.com/shrimpy/gowebsockettry

【讨论】:

  • 我们什么时候可以通过 Git 将 Go 应用部署到 Azure 了?我不知道 Kudu 可以处理 Go 应用程序?这对我来说现在改变了游戏规则。确实在通过 Git 部署时按原样工作,谢谢你的大开眼界,哇。
  • @DominicSt-Pierre 我们上个月公开宣布azure.microsoft.com/en-us/blog/…,请随时通过githubgithub.com/projectkudu/kudu向我们提交反馈
猜你喜欢
  • 2015-12-28
  • 1970-01-01
  • 1970-01-01
  • 2016-09-02
  • 2019-07-27
  • 1970-01-01
  • 2020-06-29
  • 1970-01-01
  • 2018-02-11
相关资源
最近更新 更多