Created
December 5, 2025 12:14
-
-
Save Hakkadaikon/78d942cd573d107727245c23fee1132c to your computer and use it in GitHub Desktop.
翔泳社 サンプルコード365+1 7/26 Go言語でNostrのサブスクリプション
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "golang.org/x/net/websocket" | |
| "golang.org/x/term" | |
| "os" | |
| "strings" | |
| "time" | |
| ) | |
| func main() { | |
| width, _, _ := term.GetSize(int(os.Stdout.Fd())) | |
| ws, _ := websocket.Dial("wss://relay.damus.io", "", "wss://localhost/") | |
| defer ws.Close() | |
| websocket.Message.Send(ws, `["REQ","sub1",{"kinds":[1]}]`) | |
| for { | |
| var msg []interface{} | |
| websocket.JSON.Receive(ws, &msg) | |
| if len(msg) >= 3 && msg[0] == "EVENT" && msg[1] == "sub1" { | |
| event := msg[2].(map[string]interface{}) | |
| fmt.Printf("%s\n[%s user:%s]\n\n%s\n\n", | |
| strings.Repeat("-", width), | |
| time.Unix(int64(event["created_at"].(float64)), 0).Add(9*time.Hour), | |
| event["pubkey"].(string)[:8]+"...", | |
| event["content"].(string)) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment