最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

websocket - Go how to get Mastodon Social status updates from a given user using web socket stream - Stack Overflow

matteradmin5PV0评论

I have created a script that should get Mastodon Social status updates from my user. However when I post a new status I do not see it in my Go script.

For example I started my steam at 15:53, then created a new Mastodon Social Status at 15:54, but it did not come in to the terminal:

Here Is my Go code:

package main

import (
    "fmt"
    "github/gorilla/websocket"
    "log"
    "os"
)

func StreamPublicTimelineWebSocket(instanceURL string, token string) {
    // Mastodon Streaming WebSocket API URL
    url := fmt.Sprintf("wss://%s/api/v1/streaming/user", instanceURL)
    log.Printf("streaming timeline web socket url: %s", url)

    // 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"},
        "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() {
    fmt.Println("Creating stream to Mastodon")

    // Stream
    token := "nk_HZ6......b2lDiI"
    instanceURL := "streaming.mastodon.social"

    // Start streaming the public timeline in real-time using WebSocket
    log.Println("Starting streaming from Mastodon...")
    StreamPublicTimelineWebSocket(instanceURL, token)

}

What is wrong? Why cant I get new statuses?

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far