【发布时间】: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 扩展包是否是原因。
我尝试过的事情:
从 web.config 中删除了
<webSocket enabled="false" />[同样的问题,但似乎需要更长时间才能发生添加一个标准的
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