mirror of
https://github.com/XRPLF/xrpl-dev-portal.git
synced 2025-11-20 11:45:50 +00:00
Code samples: update includes for moved samples
This commit is contained in:
@@ -363,7 +363,7 @@ WS_HANDLERS["transaction"] = log_tx
|
||||
以下のサンプルコードは、上に示したすべてのトランザクションのタイプのトランザクションメタデータを確認し、アカウントが受け取ったXRPの金額をレポートします。
|
||||
|
||||
```js
|
||||
{% include '_code-samples/monitor-payments-websocket/read-amount-received.js' %}
|
||||
{% include '_code-samples/monitor-payments-websocket/js/read-amount-received.js' %}
|
||||
```
|
||||
|
||||
{{ start_step("Read Payments") }}
|
||||
@@ -480,106 +480,10 @@ $("#tx_read").click((event) => {
|
||||
|
||||
<!-- MULTICODE_BLOCK_START -->
|
||||
|
||||
*Go*
|
||||
_Go_
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
// Connect to the XRPL Ledger using websocket and subscribe to an account
|
||||
// translation from the JavaScript example to Go
|
||||
// https://developers.ripple.com/monitor-incoming-payments-with-websocket.html
|
||||
// This example uses the Gorilla websocket library to create a websocket client
|
||||
// install: go get github.com/gorilla/websocket
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"log"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/signal"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
// websocket address
|
||||
var addr = flag.String("addr", "s.altnet.rippletest.net:51233", "http service address")
|
||||
|
||||
// Payload object
|
||||
type message struct {
|
||||
Command string `json:"command"`
|
||||
Accounts []string `json:"accounts"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
log.SetFlags(0)
|
||||
|
||||
var m message
|
||||
|
||||
// check for interrupts and cleanly close the connection
|
||||
interrupt := make(chan os.Signal, 1)
|
||||
signal.Notify(interrupt, os.Interrupt)
|
||||
|
||||
u := url.URL{Scheme: "ws", Host: *addr, Path: "/"}
|
||||
log.Printf("connecting to %s", u.String())
|
||||
|
||||
// make the connection
|
||||
c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
|
||||
if err != nil {
|
||||
log.Fatal("dial:", err)
|
||||
}
|
||||
// on exit close
|
||||
defer c.Close()
|
||||
|
||||
done := make(chan struct{})
|
||||
|
||||
// send a subscribe command and a target XRPL account
|
||||
m.Command = "subscribe"
|
||||
m.Accounts = append(m.Accounts, "rUCzEr6jrEyMpjhs4wSdQdz4g8Y382NxfM")
|
||||
|
||||
// struct to JSON marshalling
|
||||
msg, _ := json.Marshal(m)
|
||||
// write to the websocket
|
||||
err = c.WriteMessage(websocket.TextMessage, []byte(string(msg)))
|
||||
if err != nil {
|
||||
log.Println("write:", err)
|
||||
return
|
||||
}
|
||||
|
||||
// read from the websocket
|
||||
_, message, err := c.ReadMessage()
|
||||
if err != nil {
|
||||
log.Println("read:", err)
|
||||
return
|
||||
}
|
||||
// print the response from the XRP Ledger
|
||||
log.Printf("recv: %s", message)
|
||||
|
||||
// handle interrupt
|
||||
for {
|
||||
select {
|
||||
case <-done:
|
||||
return
|
||||
case <-interrupt:
|
||||
log.Println("interrupt")
|
||||
|
||||
// Cleanly close the connection by sending a close message and then
|
||||
// waiting (with timeout) 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):
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
{% include '_code-samples/monitor-payments-websocket/go/monitor-incoming-payments.go' %}
|
||||
```
|
||||
|
||||
<!-- MULTICODE_BLOCK_END -->
|
||||
|
||||
@@ -358,7 +358,7 @@ When you subscribe to an account, you get messages for _all transactions to or f
|
||||
The following sample code looks at transaction metadata of all the above transaction types to report how much XRP an account received:
|
||||
|
||||
```js
|
||||
{% include '_code-samples/monitor-payments-websocket/read-amount-received.js' %}
|
||||
{% include '_code-samples/monitor-payments-websocket/js/read-amount-received.js' %}
|
||||
```
|
||||
|
||||
{{ start_step("Read Payments") }}
|
||||
@@ -475,106 +475,10 @@ Many programming languages have libraries for sending and receiving data over a
|
||||
|
||||
<!-- MULTICODE_BLOCK_START -->
|
||||
|
||||
*Go*
|
||||
_Go_
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
// Connect to the XRPL Ledger using websocket and subscribe to an account
|
||||
// translation from the JavaScript example to Go
|
||||
// https://developers.ripple.com/monitor-incoming-payments-with-websocket.html
|
||||
// This example uses the Gorilla websocket library to create a websocket client
|
||||
// install: go get github.com/gorilla/websocket
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"log"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/signal"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
// websocket address
|
||||
var addr = flag.String("addr", "s.altnet.rippletest.net:51233", "http service address")
|
||||
|
||||
// Payload object
|
||||
type message struct {
|
||||
Command string `json:"command"`
|
||||
Accounts []string `json:"accounts"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
log.SetFlags(0)
|
||||
|
||||
var m message
|
||||
|
||||
// check for interrupts and cleanly close the connection
|
||||
interrupt := make(chan os.Signal, 1)
|
||||
signal.Notify(interrupt, os.Interrupt)
|
||||
|
||||
u := url.URL{Scheme: "ws", Host: *addr, Path: "/"}
|
||||
log.Printf("connecting to %s", u.String())
|
||||
|
||||
// make the connection
|
||||
c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
|
||||
if err != nil {
|
||||
log.Fatal("dial:", err)
|
||||
}
|
||||
// on exit close
|
||||
defer c.Close()
|
||||
|
||||
done := make(chan struct{})
|
||||
|
||||
// send a subscribe command and a target XRPL account
|
||||
m.Command = "subscribe"
|
||||
m.Accounts = append(m.Accounts, "rUCzEr6jrEyMpjhs4wSdQdz4g8Y382NxfM")
|
||||
|
||||
// struct to JSON marshalling
|
||||
msg, _ := json.Marshal(m)
|
||||
// write to the websocket
|
||||
err = c.WriteMessage(websocket.TextMessage, []byte(string(msg)))
|
||||
if err != nil {
|
||||
log.Println("write:", err)
|
||||
return
|
||||
}
|
||||
|
||||
// read from the websocket
|
||||
_, message, err := c.ReadMessage()
|
||||
if err != nil {
|
||||
log.Println("read:", err)
|
||||
return
|
||||
}
|
||||
// print the response from the XRP Ledger
|
||||
log.Printf("recv: %s", message)
|
||||
|
||||
// handle interrupt
|
||||
for {
|
||||
select {
|
||||
case <-done:
|
||||
return
|
||||
case <-interrupt:
|
||||
log.Println("interrupt")
|
||||
|
||||
// Cleanly close the connection by sending a close message and then
|
||||
// waiting (with timeout) 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):
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
{% include '_code-samples/monitor-payments-websocket/go/monitor-incoming-payments.go' %}
|
||||
```
|
||||
|
||||
<!-- MULTICODE_BLOCK_END -->
|
||||
|
||||
Reference in New Issue
Block a user