gpt4 book ai didi

node.js - Websocket 连接断开使用 Golang 的 gorilla/websocket 包

转载 作者:IT王子 更新时间:2023-10-29 01:04:24 24 4
gpt4 key购买 nike

我正在尝试使用 gorilla/websocket 将建立和维护与第三方服务器的 Websocket 连接的 NodeJS 脚本移植到 Go。包裹。在Node script ,在 ping 之后收到一个 pong,并且连接无限期地保持事件状态。在Go script ,ping/pong 工作,但连接在大约 30 秒后被服务器断开。

我怀疑使用 Go websocket 包发送的 ping 格式不正确,但我无法查明原因。在运行这些程序时比较捕获的加密网络流量表明 TCP 请求和响应的响应长度没有差异,因此这可能不是问题所在。任何帮助将不胜感激!

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
}
}
}

最佳答案

两者的地址不同。 Node 代码正在与

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

go 代码正在与:

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

看起来它们解析为不同的 IP,因此它们可能具有不同的入口基础结构,并且 api 子域上存在 ws 子域上不存在的超时。

关于node.js - Websocket 连接断开使用 Golang 的 gorilla/websocket 包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49373582/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com