Azure WebSocket在使用Go的httpPlatformHandler时无法工作。

huangapple go评论128阅读模式
英文:

Azure WebSocket not working with Go httpPlatformHandler

问题

我在Azure Web App上使用WebSocket遇到了困难,我正在将一个Node应用程序从Azure迁移到Go应用程序。

WebSocket在门户上已启用,这是我的web.config配置文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <configuration>
  3. <system.web>
  4. <customErrors mode="Off"/>
  5. </system.web>
  6. <system.webServer>
  7. <webSocket enabled="false" />
  8. <handlers>
  9. <add name="httpplatformhandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" />
  10. </handlers>
  11. <httpPlatform stdoutLogEnabled="true" processPath="d:\home\site\wwwroot\v-0.0.10.exe"
  12. arguments=""
  13. startupTimeLimit="60">
  14. <environmentVariables>
  15. <environmentVariable name="GOROOT" value="d:\home\site\wwwroot\go" />
  16. </environmentVariables>
  17. </httpPlatform>
  18. </system.webServer>
  19. </configuration>

当然,在本地它工作正常。起初,我只尝试了这个示例:

  1. package main
  2. import (
  3. "code.google.com/p/go.net/websocket"
  4. "io"
  5. "net/http"
  6. "os"
  7. )
  8. func echoHandler(ws *websocket.Conn) {
  9. io.Copy(ws, ws)
  10. }
  11. func main() {
  12. http.Handle("/echo", websocket.Handler(echoHandler))
  13. http.Handle("/", http.FileServer(http.Dir(".")))
  14. err := http.ListenAndServe(":"+os.Getevn("HTTP_PLATFORM_PORT"), nil)
  15. if err != nil {
  16. panic("ListenAndServe: " + err.Error())
  17. }
  18. }

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

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

在部署到Azure后,readyState始终保持为0,onopen从未被调用。最终失败,并显示连接错误:

  1. 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来提供一个“标准”页面,它可以正常工作。

我在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

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  2. &lt;configuration&gt;
  3. &lt;system.web&gt;
  4. &lt;customErrors mode=&quot;Off&quot;/&gt;
  5. &lt;/system.web&gt;
  6. &lt;system.webServer&gt;
  7. &lt;webSocket enabled=&quot;false&quot; /&gt;
  8. &lt;handlers&gt;
  9. &lt;add name=&quot;httpplatformhandler&quot; path=&quot;*&quot; verb=&quot;*&quot; modules=&quot;httpPlatformHandler&quot; resourceType=&quot;Unspecified&quot; /&gt;
  10. &lt;/handlers&gt;
  11. &lt;httpPlatform stdoutLogEnabled=&quot;true&quot; processPath=&quot;d:\home\site\wwwroot\v-0.0.10.exe&quot;
  12. arguments=&quot;&quot;
  13. startupTimeLimit=&quot;60&quot;&gt;
  14. &lt;environmentVariables&gt;
  15. &lt;environmentVariable name=&quot;GOROOT&quot; value=&quot;d:\home\site\wwwroot\go&quot; /&gt;
  16. &lt;/environmentVariables&gt;
  17. &lt;/httpPlatform&gt;
  18. &lt;/system.webServer&gt;
  19. &lt;/configuration&gt;

Of course locally it's working fine. At first I only tried this sample:

  1. package main
  2. import (
  3. &quot;code.google.com/p/go.net/websocket&quot;
  4. &quot;io&quot;
  5. &quot;net/http&quot;
  6. &quot;os&quot;
  7. )
  8. func echoHandler(ws *websocket.Conn) {
  9. io.Copy(ws, ws)
  10. }
  11. func main() {
  12. http.Handle(&quot;/echo&quot;, websocket.Handler(echoHandler))
  13. http.Handle(&quot;/&quot;, http.FileServer(http.Dir(&quot;.&quot;)))
  14. err := http.ListenAndServe(&quot;:&quot;+os.Getevn(&quot;HTTP_PLATFORM_PORT&quot;), nil)
  15. if err != nil {
  16. panic(&quot;ListenAndServe: &quot; + err.Error())
  17. }
  18. }

On the client side I simply try to connect:

  1. var ws = new WebSocket(&quot;url&quot;)
  2. ws.onopen = function() { ws.send(&quot;test&quot;); }
  3. 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:

  1. WebSocket connection to &#39;wss://***.azurewebsites.net/echo&#39; 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:

  1. Removed the &lt;webSocket enabled=&quot;false&quot; /&gt; from web.config [same issue but it appears to take longer before occuring

  2. 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

Azure WebSocket在使用Go的httpPlatformHandler时无法工作。

英文:

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

Azure WebSocket在使用Go的httpPlatformHandler时无法工作。

huangapple
  • 本文由 发表于 2015年11月12日 23:40:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/33675348.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定