doumeng2637 2016-04-01 12:50
浏览 418

带有Web代理的Golang Gorilla Websocket

I'm working on a POC using Gorilla/Websocket to communicate, through a Web Proxy, with the Websocket Check Website "echo.websocket.org".

I'm using the free online Proxy "hide.me/en/" for testing.

When I simply try to communicate with "echo.websocket.org" (Server side), my client-site Websocket POC reach the response.

But when i try to add the Proxy gesture, everything goes wrong :(

Here is a sample of my code :

package main

import (
    "flag"
    "log"
    "net/url"
    "os"
    "os/signal"
    "time"

    "github.com/gorilla/websocket"
    "net/http"
)

var addrWebsocket = flag.String("addrWebsocket", "echo.websocket.org", "http service address")

func main() {
    flag.Parse()
    log.SetFlags(0)

    interrupt := make(chan os.Signal, 1)
    signal.Notify(interrupt, os.Interrupt)

    //Initialize the WebSocket URL and the Path to follow
    uWS := url.URL{Scheme: "wss", Host: *addrWebsocket}

    //Initialize the Proxy URL and the Path to follow
    uProxy, _ := url.Parse("https://hide.me/en/proxy")

    //Set the Dialer (especially the proxy)
    dialer := websocket.Dialer{
        Proxy: http.ProxyURL(uProxy),
    }
    //dialer := websocket.DefaultDialer ==> with this default dialer, it works !

    c, _, err := dialer.Dial(uWS.String(), nil) // ==> With the proxy config, it fails here !
    defer c.Close()

    done := make(chan struct{})

    go func() {
        defer c.Close()
        defer close(done)
        for {
            _, message, err := c.ReadMessage()
            if err != nil {
                log.Println("read:", err)
                return
            }
            log.Printf("recv: %s", message)
        }
    }()

    ticker := time.NewTicker(time.Second)
    defer ticker.Stop()

    for {
        select {
        case t := <-ticker.C:
            err := c.WriteMessage(websocket.TextMessage, []byte(t.String()))
            if err != nil {
                log.Println("write:", err)
                return
            }
        case <-interrupt:
            log.Println("interrupt")
            err := c.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
            if err != nil {
                log.Println("write close:", err)
                return
            }
                select {
                case <-done:
                case <-time.After(time.Second):
                }
            c.Close()
            return
        }
    }
}

I'm a very beginner in Go so don't hesitate to correct me if my code un not clear and clean.

Thanks for your help !

  • 写回答

1条回答 默认 最新

  • douduxia1551 2016-04-01 20:36
    关注

    The code is correct. The server https://hide.me is not a proxy server.

    评论

报告相同问题?

悬赏问题

  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?