Azure WebSocket无法与Go httpPlatformHandler一起使用

Azure WebSocket无法与Go httpPlatformHandler一起使用

问题描述:

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:

  1. Removed the <webSocket enabled="false" /> 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

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

enter image description here