我正在尝试使用 WebSockets 实时传输来自 Mastodon 的公共帖子,并使用 API 检索公共帖子。但是,我在使用 WebSocket 时遇到了问题
bad handshake
错误。
如何使用 WebSocket 实时传输 Mastodon 的公共帖子?我需要遵循其他配置或步骤吗?
WebSocket URL
我首先使用以下 WebSocket URL 连接到 mastodon.social 上的公共时间线流:
wss://mastodon.social/api/v1/streaming/public
这会导致错误:
websocket: bad handshake
网络socat
使用 websocat 测试:
websocat wss://echo.websocket.org
这工作正常并返回消息。
通过 HTTP 测试公共时间线
我使用 Mastodon HTTP API 从时间线中检索公共帖子,效果非常好:
curl https://mastodon.social/api/v1/timelines/public
扫码:
package main
import (
"fmt"
"log"
"os"
"github.com/gorilla/websocket"
)
func streamPublicTimelineWebSocket(instanceURL string) {
// Updated Mastodon Streaming WebSocket API URL for public posts
url := fmt.Sprintf("wss://%s/api/v1/streaming/public", instanceURL)
// Create a WebSocket dialer to manage the connection
dialer := websocket.DefaultDialer
// Create a map for custom headers if necessary (e.g., User-Agent)
headers := map[string][]string{
"User-Agent": {"Mastodon Streamer Go"},
}
// Establish a WebSocket connection with custom headers
conn, _, err := dialer.Dial(url, headers)
if err != nil {
log.Fatalf("Failed to connect to Mastodon streaming WebSocket: %v", err)
os.Exit(1)
}
defer conn.Close()
log.Printf("Successfully connected to stream at: %s", url)
// Continuously read and process incoming WebSocket messages
for {
_, msg, err := conn.ReadMessage()
if err != nil {
log.Fatalf("Error reading WebSocket message: %v", err)
}
// Print out the received message (which is a Mastodon post)
log.Printf("Received message: %s", msg)
}
}
func main() {
// Replace with your Mastodon instance URL (e.g., "streaming.mastodon.social")
instanceURL := "streaming.mastodon.social" // Updated to the correct WebSocket URL
// Start streaming the public timeline in real-time using WebSocket
log.Println("Starting streaming from Mastodon...")
streamPublicTimelineWebSocket(instanceURL)
}
您必须在帐户外观->开发->新应用程序中创建令牌。复制 de token 并将 token 添加到 head 中。
package main
import (
"fmt"
"log"
"os"
"github.com/gorilla/websocket"
)
func streamPublicTimelineWebSocket(instanceURL string) {
// Updated Mastodon Streaming WebSocket API URL for public posts
url := fmt.Sprintf("wss://%s/api/v1/streaming/public", instanceURL)
// Create a WebSocket dialer to manage the connection
dialer := websocket.DefaultDialer
token := "you token"
// Create a map for custom headers if necessary (e.g., User-Agent)
headers := map[string][]string{
"User-Agent": {"Mastodon Streamer Go"},
"Authorization": {" Bearer " + token},
}
// Establish a WebSocket connection with custom headers
conn, _, err := dialer.Dial(url, headers)
if err != nil {
log.Fatalf("Failed to connect to Mastodon streaming WebSocket: %v", err)
os.Exit(1)
}
defer conn.Close()
log.Printf("Successfully connected to stream at: %s", url)
// Continuously read and process incoming WebSocket messages
for {
_, msg, err := conn.ReadMessage()
if err != nil {
log.Fatalf("Error reading WebSocket message: %v", err)
}
// Print out the received message (which is a Mastodon post)
log.Printf("Received message: %s", msg)
}
}
func main() {
// Replace with your Mastodon instance URL (e.g., "streaming.mastodon.social")
instanceURL := "streaming.mastodon.social" // Updated to the correct WebSocket URL
// Start streaming the public timeline in real-time using WebSocket
log.Println("Starting streaming from Mastodon...")
streamPublicTimelineWebSocket(instanceURL)
}