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

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

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扩展包的问题。

我尝试过的事情:

  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

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;configuration&gt;
  &lt;system.web&gt;
        &lt;customErrors mode=&quot;Off&quot;/&gt;
    &lt;/system.web&gt;
    &lt;system.webServer&gt;
        &lt;webSocket enabled=&quot;false&quot; /&gt;
        &lt;handlers&gt;
            &lt;add name=&quot;httpplatformhandler&quot; path=&quot;*&quot; verb=&quot;*&quot; modules=&quot;httpPlatformHandler&quot; resourceType=&quot;Unspecified&quot; /&gt;
        &lt;/handlers&gt;
        &lt;httpPlatform stdoutLogEnabled=&quot;true&quot; processPath=&quot;d:\home\site\wwwroot\v-0.0.10.exe&quot;
                      arguments=&quot;&quot;
                      startupTimeLimit=&quot;60&quot;&gt;
            &lt;environmentVariables&gt;
              &lt;environmentVariable name=&quot;GOROOT&quot; value=&quot;d:\home\site\wwwroot\go&quot; /&gt;
            &lt;/environmentVariables&gt;
        &lt;/httpPlatform&gt;
    &lt;/system.webServer&gt;
&lt;/configuration&gt;

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

package main

import (
	&quot;code.google.com/p/go.net/websocket&quot;
	&quot;io&quot;
	&quot;net/http&quot;
    &quot;os&quot;
)

func echoHandler(ws *websocket.Conn) {
	io.Copy(ws, ws)
}

func main() {
	http.Handle(&quot;/echo&quot;, websocket.Handler(echoHandler))
	http.Handle(&quot;/&quot;, http.FileServer(http.Dir(&quot;.&quot;)))
	err := http.ListenAndServe(&quot;:&quot;+os.Getevn(&quot;HTTP_PLATFORM_PORT&quot;), nil)
	if err != nil {
		panic(&quot;ListenAndServe: &quot; + err.Error())
	}
}

On the client side I simply try to connect:

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

确定