admin管理员组文章数量:1025478
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?
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?
本文标签:
版权声明:本文标题:websocket - Go how to get Mastodon Social status updates from a given user using web socket stream - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745636467a2160479.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论