英文:
Azure WebSocket not working with Go httpPlatformHandler
问题
我在Azure Web App上使用WebSocket遇到了困难,我正在将一个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
来提供一个“标准”页面,它可以正常工作。
我在Azure上部署了一些Go应用程序,只是第一个需要使用WebSocket的应用程序,我想知道我做错了什么/没有理解的地方。
非常感谢任何帮助。
英文:
I'm having a hard time making websocket works on an Azure Web App, I'm currently porting a Node app from Azure to a Go app.
WebSocket is enable on the portal and here's my 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>
Of course locally it's working fine. At first I only tried this sample:
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())
}
}
On the client side I simply try to connect:
var ws = new WebSocket("url")
ws.onopen = function() { ws.send("test"); }
ws.onmessage = function(e) { console.log(e.data); }
When deployed on Azure, the readystate always stays at 0, onopen is never call. It ultimately failed saying there's an error connecting:
WebSocket connection to 'wss://***.azurewebsites.net/echo' failed: Error during WebSocket handshake: Unexpected response code: 500
I even tried with Gorilla Websocket example to see if the Go extended package was in cause.
Things I tried:
-
Removed the
<webSocket enabled="false" />
from web.config [same issue but it appears to take longer before occuring -
Adding a standard
http.HandleFunc
to serve a "standard" page and it works fine.
I have a handful of Go app deploy to Azure, it's just the first one that I would need to use websocket, and I would like to know what I'm doing wrong / not understanding.
Any help would be greatly appreciated
答案1
得分: 1
我参考了你的代码并创建了一个示例,它对我来说有效。我没有做任何自定义的web.config,而是使用了来自Azure部署脚本的默认配置。
这是示例仓库的链接:https://github.com/shrimpy/gowebsockettry
英文:
I reference your code and created a sample, it is working for me.
I didn`t do any custom web.config but using the default one come from Azure Deployment script.
here is the sample repo: https://github.com/shrimpy/gowebsockettry
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论