mirror of
https://github.com/XRPLF/xrpl-dev-portal.git
synced 2025-11-21 20:25:51 +00:00
Code samples: update includes for moved samples
This commit is contained in:
@@ -0,0 +1,97 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
// Connect to the XRPL Ledger using websocket and subscribe to an account
|
||||||
|
// translation from the JavaScript example to Go
|
||||||
|
// https://xrpl.org/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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -45,13 +45,13 @@ curated_anchors:
|
|||||||
**JSON:**
|
**JSON:**
|
||||||
|
|
||||||
```json
|
```json
|
||||||
{% include '_code-samples/tx-serialization/test-cases/tx1.json' %}
|
{% include '_code-samples/tx-serialization/py/test-cases/tx1.json' %}
|
||||||
```
|
```
|
||||||
|
|
||||||
**バイナリ(16進数として表現):**
|
**バイナリ(16進数として表現):**
|
||||||
|
|
||||||
```text
|
```text
|
||||||
{% include '_code-samples/tx-serialization/test-cases/tx1-binary.txt' %}
|
{% include '_code-samples/tx-serialization/py/test-cases/tx1-binary.txt' %}
|
||||||
```
|
```
|
||||||
|
|
||||||
## サンプルコード
|
## サンプルコード
|
||||||
@@ -60,7 +60,7 @@ curated_anchors:
|
|||||||
|
|
||||||
- C++: [`rippled`コードベース](https://github.com/ripple/rippled/blob/develop/src/ripple/protocol/impl/STObject.cpp)
|
- C++: [`rippled`コードベース](https://github.com/ripple/rippled/blob/develop/src/ripple/protocol/impl/STObject.cpp)
|
||||||
- JavaScript: [`ripple-binary-codec`](https://github.com/ripple/ripple-binary-codec/)パッケージ
|
- JavaScript: [`ripple-binary-codec`](https://github.com/ripple/ripple-binary-codec/)パッケージ
|
||||||
- Python 3: [このリポジトリのコードサンプルセクション]({{target.github_forkurl}}/blob/{{target.github_branch}}/content/_code-samples/tx-serialization/serialize.py)
|
- Python 3: [このリポジトリのコードサンプルセクション]({{target.github_forkurl}}/blob/{{target.github_branch}}/content/_code-samples/tx-serialization/)
|
||||||
|
|
||||||
これらのすべての実装には、一般利用が可能なオープンソースライセンスが提供されているので、学習のためにドキュメントと合わせて使用するだけでなく、必要に応じてコードをインポート、使用、または変更することができます。
|
これらのすべての実装には、一般利用が可能なオープンソースライセンスが提供されているので、学習のためにドキュメントと合わせて使用するだけでなく、必要に応じてコードをインポート、使用、または変更することができます。
|
||||||
|
|
||||||
|
|||||||
@@ -45,13 +45,13 @@ Both signed and unsigned transactions can be represented in both JSON and binary
|
|||||||
**JSON:**
|
**JSON:**
|
||||||
|
|
||||||
```json
|
```json
|
||||||
{% include '_code-samples/tx-serialization/test-cases/tx1.json' %}
|
{% include '_code-samples/tx-serialization/py/test-cases/tx1.json' %}
|
||||||
```
|
```
|
||||||
|
|
||||||
**Binary (represented as hexadecimal):**
|
**Binary (represented as hexadecimal):**
|
||||||
|
|
||||||
```text
|
```text
|
||||||
{% include '_code-samples/tx-serialization/test-cases/tx1-binary.txt' %}
|
{% include '_code-samples/tx-serialization/py/test-cases/tx1-binary.txt' %}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Sample Code
|
## Sample Code
|
||||||
@@ -60,7 +60,7 @@ The serialization processes described here are implemented in multiple places an
|
|||||||
|
|
||||||
- In C++ [in the `rippled` code base](https://github.com/ripple/rippled/blob/develop/src/ripple/protocol/impl/STObject.cpp).
|
- In C++ [in the `rippled` code base](https://github.com/ripple/rippled/blob/develop/src/ripple/protocol/impl/STObject.cpp).
|
||||||
- In JavaScript in the [`ripple-binary-codec`](https://github.com/ripple/ripple-binary-codec/) package.
|
- In JavaScript in the [`ripple-binary-codec`](https://github.com/ripple/ripple-binary-codec/) package.
|
||||||
- In Python 3 in [this repository's code samples section]({{target.github_forkurl}}/blob/{{target.github_branch}}/content/_code-samples/tx-serialization/serialize.py).
|
- In Python 3 in [this repository's code samples section]({{target.github_forkurl}}/blob/{{target.github_branch}}/content/_code-samples/tx-serialization/).
|
||||||
|
|
||||||
Additionally, many [client libraries](client-libraries.html) provide serialization support under permissive open-source licenses, so you can import, use, or adapt the code for your needs.
|
Additionally, many [client libraries](client-libraries.html) provide serialization support under permissive open-source licenses, so you can import, use, or adapt the code for your needs.
|
||||||
|
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ Some fields that may appear in transaction metadata include:
|
|||||||
The following JSON object shows the metadata for [a complex cross-currency payment](https://xrpcharts.ripple.com/#/transactions/8C55AFC2A2AA42B5CE624AEECDB3ACFDD1E5379D4E5BF74A8460C5E97EF8706B):
|
The following JSON object shows the metadata for [a complex cross-currency payment](https://xrpcharts.ripple.com/#/transactions/8C55AFC2A2AA42B5CE624AEECDB3ACFDD1E5379D4E5BF74A8460C5E97EF8706B):
|
||||||
|
|
||||||
```json
|
```json
|
||||||
{% include '_code-samples/metadata/cross-currency-payment.json' %}
|
{% include '_api-examples/metadata/cross-currency-payment.json' %}
|
||||||
```
|
```
|
||||||
|
|
||||||
## AffectedNodes
|
## AffectedNodes
|
||||||
|
|||||||
@@ -84,7 +84,7 @@ rippled -a [OPTIONS]
|
|||||||
| オプション | 説明 |
|
| オプション | 説明 |
|
||||||
|:----------------------|:-----------------------------------------------------|
|
|:----------------------|:-----------------------------------------------------|
|
||||||
| `--ledger {LEDGER}` | `{LEDGER}`(レジャーハッシュまたはレジャーインデックス)により初期レジャーと識別されているレジャーバージョンを読み込みます。指定されたレジャーバージョンは、サーバーのレジャーストアーに格納される必要があります。 |
|
| `--ledger {LEDGER}` | `{LEDGER}`(レジャーハッシュまたはレジャーインデックス)により初期レジャーと識別されているレジャーバージョンを読み込みます。指定されたレジャーバージョンは、サーバーのレジャーストアーに格納される必要があります。 |
|
||||||
| `--ledgerfile {FILE}` | 指定された`{FILE}`からレジャーバージョンを読み込みます(このファイルには完全なレジャーがJSONフォーマットで格納されている必要があります)。このようなファイルの例については、付属の[`ledger-file.json`]({{target.github_forkurl}}/blob/{{target.github_branch}}/content/_code-samples/rippled-cli/ledger-file.json)を参照してください。 |
|
| `--ledgerfile {FILE}` | 指定された`{FILE}`からレジャーバージョンを読み込みます(このファイルには完全なレジャーがJSONフォーマットで格納されている必要があります)。このようなファイルの例については、付属の[`ledger-file.json`]({{target.github_forkurl}}/blob/{{target.github_branch}}/content/_api-examples/rippled-cli/ledger-file.json)を参照してください。 |
|
||||||
| `--load` | **廃止予定** デバッグのためのオプションです。ディスク上のレジャーストアーから初期レジャーを読み込むだけです。 |
|
| `--load` | **廃止予定** デバッグのためのオプションです。ディスク上のレジャーストアーから初期レジャーを読み込むだけです。 |
|
||||||
| `--replay` | デバッグのためのオプションです。`--ledger`と組み合わせて使用し、レジャーの閉鎖をリプレイします。サーバーのレジャーストアーには、当該レジャーとその直前のバージョンのレジャーがすでに格納されている必要があります。サーバーでは、前のレジャーをベースとして使用して、指定されたレジャーのすべてのトランザクションが処理されます。その結果、指定されたレジャーが再作成されます。デバッガーを使用して、特定のトランザクションの処理ロジックを分析するためのブレークポイントを追加できます。 |
|
| `--replay` | デバッグのためのオプションです。`--ledger`と組み合わせて使用し、レジャーの閉鎖をリプレイします。サーバーのレジャーストアーには、当該レジャーとその直前のバージョンのレジャーがすでに格納されている必要があります。サーバーでは、前のレジャーをベースとして使用して、指定されたレジャーのすべてのトランザクションが処理されます。その結果、指定されたレジャーが再作成されます。デバッガーを使用して、特定のトランザクションの処理ロジックを分析するためのブレークポイントを追加できます。 |
|
||||||
| `--start` | デバッグのためのオプションです。既知のすべてのAmendment(反対票を投じるようにサーバーに設定されているAmendmentを除く)が適用されている新しいジェネシスレジャーを使用して開始します。したがってこれらのAmendmentの機能は、2週間の[Amendmentプロセス](amendments.html)期間ではなく、2番目のレジャーの開始時から使用可能になります。 |
|
| `--start` | デバッグのためのオプションです。既知のすべてのAmendment(反対票を投じるようにサーバーに設定されているAmendmentを除く)が適用されている新しいジェネシスレジャーを使用して開始します。したがってこれらのAmendmentの機能は、2週間の[Amendmentプロセス](amendments.html)期間ではなく、2番目のレジャーの開始時から使用可能になります。 |
|
||||||
|
|||||||
@@ -83,7 +83,7 @@ The following options determine which ledger to load first when starting up. The
|
|||||||
| Option | Description |
|
| Option | Description |
|
||||||
|:----------------------|:-----------------------------------------------------|
|
|:----------------------|:-----------------------------------------------------|
|
||||||
| `--ledger {LEDGER}` | Load the ledger version identified by `{LEDGER}` (either a ledger hash or a ledger index) as the initial ledger. The specified ledger version must be in the server's ledger store. |
|
| `--ledger {LEDGER}` | Load the ledger version identified by `{LEDGER}` (either a ledger hash or a ledger index) as the initial ledger. The specified ledger version must be in the server's ledger store. |
|
||||||
| `--ledgerfile {FILE}` | Load the ledger version from the specified `{FILE}`, which must contain a complete ledger in JSON format. For an example of such a file, see the provided [`ledger-file.json`]({{target.github_forkurl}}/blob/{{target.github_branch}}/content/_code-samples/rippled-cli/ledger-file.json). |
|
| `--ledgerfile {FILE}` | Load the ledger version from the specified `{FILE}`, which must contain a complete ledger in JSON format. For an example of such a file, see the provided [`ledger-file.json`]({{target.github_forkurl}}/blob/{{target.github_branch}}/content/_api-examples/rippled-cli/ledger-file.json). |
|
||||||
| `--load` | **DEPRECATED** Intended for debugging. Only load the initial ledger from the ledger store on disk. |
|
| `--load` | **DEPRECATED** Intended for debugging. Only load the initial ledger from the ledger store on disk. |
|
||||||
| `--replay` | Intended for debugging. Use with `--ledger` to replay a ledger close. Your server must have the ledger in question and its direct ancestor already in the ledger store. Using the previous ledger as a base, the server processes all the transactions in the specified ledger, resulting in a re-creation of the specified ledger. With a debugger, you can add breakpoints to analyze specific transaction processing logic. |
|
| `--replay` | Intended for debugging. Use with `--ledger` to replay a ledger close. Your server must have the ledger in question and its direct ancestor already in the ledger store. Using the previous ledger as a base, the server processes all the transactions in the specified ledger, resulting in a re-creation of the specified ledger. With a debugger, you can add breakpoints to analyze specific transaction processing logic. |
|
||||||
| `--start` | Intended for debugging. Start with a new genesis ledger that has all known amendments (except those the server is configured to vote against) enabled. The functionality of those amendments is therefore available starting from the second ledger, rather than going through the full two-week [Amendment Process](amendments.html). |
|
| `--start` | Intended for debugging. Start with a new genesis ledger that has all known amendments (except those the server is configured to vote against) enabled. The functionality of those amendments is therefore available starting from the second ledger, rather than going through the full two-week [Amendment Process](amendments.html). |
|
||||||
|
|||||||
@@ -76,7 +76,7 @@ Response:
|
|||||||
```json
|
```json
|
||||||
200 OK
|
200 OK
|
||||||
|
|
||||||
{% include '_code-samples/peer-crawler/crawl.json' %}
|
{% include '_api-examples/peer-crawler/crawl.json' %}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -87,7 +87,7 @@ Each member of the `validators` array has the following fields:
|
|||||||
#### Example Decoded Blob
|
#### Example Decoded Blob
|
||||||
|
|
||||||
```json
|
```json
|
||||||
{% include '_code-samples/vl/vl-blob.json' %}
|
{% include '_api-examples/vl/vl-blob.json' %}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Example
|
## Example
|
||||||
@@ -115,7 +115,7 @@ Response:
|
|||||||
```json
|
```json
|
||||||
200 OK
|
200 OK
|
||||||
|
|
||||||
{% include '_code-samples/vl/vl.json' %}
|
{% include '_api-examples/vl/vl.json' %}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -363,7 +363,7 @@ WS_HANDLERS["transaction"] = log_tx
|
|||||||
以下のサンプルコードは、上に示したすべてのトランザクションのタイプのトランザクションメタデータを確認し、アカウントが受け取ったXRPの金額をレポートします。
|
以下のサンプルコードは、上に示したすべてのトランザクションのタイプのトランザクションメタデータを確認し、アカウントが受け取ったXRPの金額をレポートします。
|
||||||
|
|
||||||
```js
|
```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") }}
|
{{ start_step("Read Payments") }}
|
||||||
@@ -480,106 +480,10 @@ $("#tx_read").click((event) => {
|
|||||||
|
|
||||||
<!-- MULTICODE_BLOCK_START -->
|
<!-- MULTICODE_BLOCK_START -->
|
||||||
|
|
||||||
*Go*
|
_Go_
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
{% include '_code-samples/monitor-payments-websocket/go/monitor-incoming-payments.go' %}
|
||||||
|
|
||||||
// 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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
<!-- MULTICODE_BLOCK_END -->
|
<!-- 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:
|
The following sample code looks at transaction metadata of all the above transaction types to report how much XRP an account received:
|
||||||
|
|
||||||
```js
|
```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") }}
|
{{ start_step("Read Payments") }}
|
||||||
@@ -475,106 +475,10 @@ Many programming languages have libraries for sending and receiving data over a
|
|||||||
|
|
||||||
<!-- MULTICODE_BLOCK_START -->
|
<!-- MULTICODE_BLOCK_START -->
|
||||||
|
|
||||||
*Go*
|
_Go_
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
{% include '_code-samples/monitor-payments-websocket/go/monitor-incoming-payments.go' %}
|
||||||
|
|
||||||
// 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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
<!-- MULTICODE_BLOCK_END -->
|
<!-- MULTICODE_BLOCK_END -->
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ For example:
|
|||||||
|
|
||||||
_JavaScript_
|
_JavaScript_
|
||||||
|
|
||||||
{{ include_code("_code-samples/require-destination-tags/require-destination-tags.js", language="js", start_with="// Send AccountSet", end_before="// Confirm Account") }}
|
{{ include_code("_code-samples/require-destination-tags/js/require-destination-tags.js", language="js", start_with="// Send AccountSet", end_before="// Confirm Account") }}
|
||||||
|
|
||||||
<!-- MULTICODE_BLOCK_END -->
|
<!-- MULTICODE_BLOCK_END -->
|
||||||
|
|
||||||
@@ -88,7 +88,7 @@ After the transaction is validated, you can check your account's settings to con
|
|||||||
|
|
||||||
_JavaScript_
|
_JavaScript_
|
||||||
|
|
||||||
{{ include_code("_code-samples/require-destination-tags/require-destination-tags.js", language="js", start_with="// Confirm Account", end_before="// End main()") }}
|
{{ include_code("_code-samples/require-destination-tags/js/require-destination-tags.js", language="js", start_with="// Confirm Account", end_before="// End main()") }}
|
||||||
|
|
||||||
<!-- MULTICODE_BLOCK_END -->
|
<!-- MULTICODE_BLOCK_END -->
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user