dongzai3139 2018-03-19 22:42
浏览 1357
已采纳

使用Golang的gorilla / websocket软件包的Websocket连接断开

I'm attempting to port a NodeJS script that establishes and maintains a Websocket connection to a third-party server to Go using the gorilla/websocket package. In the Node script, a pong is received following a ping, and the connection is kept alive indefinitely. In the Go script, ping/pong works but the connection is dropped by the server after about 30 seconds.

I suspect that the pings that are sent using the Go websocket package are malformed, but I can't pinpoint the cause of this. Comparing the captured, encrypted network traffic while running these programs shows no difference in the response length of the TCP requests and responses, so this may not be the issue. Any help would be greatly appreciated!

websocket.js

#!/usr/bin/env node

// npm install websocket@1.0.25 --save
const WebSocketClient = require('websocket').client;
const client = new WebSocketClient();

let lastPing = new Date().getTime();

client.on('connectFailed', function(error) {
    console.log('Connect Error: ' + error.toString());
});

client.on('connect', function(connection) {
    console.log('Connected to Server...');
    connection.on('error', function(error) {
        console.log("Connection Error: " + error.toString());
    });
    connection.on('close', function() {
        console.log('Connection Closed');
    });
    connection.on('message', function(message) {
      if (message.type === 'utf8') {
        console.log(message.utf8Data);
      }
    });
    connection.on('pong', function(){
      console.log('[pingpong] response took', (new Date().getTime() - lastPing) + 'ms');
    })

    function send(message) {
      if (connection.connected) {
          connection.sendUTF(message);
      }
    }

    // Send a ping every 10s
    // to keep the connection live
    setInterval(function(){
      lastPing = new Date().getTime();
      connection.ping();
    }, 10000);
});

client.connect('wss://ws.radarrelay.com/0x/v0/ws');

websocket.go

package main

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

    "github.com/gorilla/websocket"
)

var addr = "wss://api.radarrelay.com/0x/v0/ws"

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

    timeoutDuration := 2 * time.Minute

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

    c, _, err := websocket.DefaultDialer.Dial(addr, nil)
    if err != nil {
        log.Fatal("dial:", err)
    } else {
        log.Println("Connected to server")
    }

    c.SetPongHandler(func(str string) error {
        log.Println("pong received", str)
        return nil
    })

    defer c.Close()

    done := make(chan struct{})

    go func() {
        defer c.Close()
        defer close(done)
        for {
            c.SetReadDeadline(time.Now().Add(timeoutDuration))
            _, message, err := c.ReadMessage()
            if err != nil {
                log.Println("read:", err)
                return
            }
            if len(message) >= 2 {
                message = message[2:]
            }
            log.Printf("recv: %s", message)
        }
    }()

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

    for {
        select {
        case _ = <-ticker.C:
            err := c.WriteMessage(websocket.PingMessage, []byte{})
            if err != nil {
                log.Println("write:", err)
                return
            } else {
                log.Println("ping sent")
            }
        case <-interrupt:
            log.Println("interrupt")
            // To cleanly close a connection, a client should send a close
            // frame and wait for the server to close the connection.
            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
        }
    }
}
  • 写回答

1条回答

  • douyou7797 2018-05-02 15:39
    关注

    The addr is different between the two. The node code is talking to

    wss://ws.radarrelay.com/0x/v0/ws
    

    And the go code is talking to:

    wss://api.radarrelay.com/0x/v0/ws
    

    Looks like those resolve to different IPs, so perhaps they have different ingress infrastructure, and there is a timeout on the api subdomain that doesn't exist on the ws subdomain.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)