Skip to content

Commit

Permalink
Add "ping" message to the websocket interface trezor#238
Browse files Browse the repository at this point in the history
  • Loading branch information
martinboehm committed Jul 19, 2019
1 parent 8b9fe50 commit 4ba0769
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,7 @@ The websocket interface provides the following requests:
- getTransactionSpecific
- estimateFee
- sendTransaction
- ping

The client can subscribe to the following events:

Expand Down
7 changes: 7 additions & 0 deletions server/public_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,13 @@ func websocketTestsBitcoinType(t *testing.T, ts *httptest.Server) {
},
want: `{"id":"15","data":{"subscribed":false}}`,
},
{
name: "websocket ping",
req: websocketReq{
Method: "ping",
},
want: `{"id":"16","data":{}}`,
},
}

// send all requests at once
Expand Down
4 changes: 4 additions & 0 deletions server/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,10 @@ var requestHandlers = map[string]func(*WebsocketServer, *websocketChannel, *webs
"unsubscribeAddresses": func(s *WebsocketServer, c *websocketChannel, req *websocketReq) (rv interface{}, err error) {
return s.unsubscribeAddresses(c)
},
"ping": func(s *WebsocketServer, c *websocketChannel, req *websocketReq) (rv interface{}, err error) {
r := struct{}{}
return r, nil
},
}

func sendResponse(c *websocketChannel, req *websocketReq, data interface{}) {
Expand Down
16 changes: 16 additions & 0 deletions static/test-websocket.html
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@
});
}

function ping() {
const method = 'ping';
const params = {
};
send(method, params, function (result) {
document.getElementById('pingResult').innerText = JSON.stringify(result).replace(/,/g, ", ");
});
}

function getBlockHash() {
const method = 'getBlockHash';
const height = parseInt(document.getElementById("getBlockHashHeight").value);
Expand Down Expand Up @@ -296,6 +305,13 @@ <h1>Blockbook Websocket Test Page</h1>
<div class="col-10" id="getInfoResult">
</div>
</div>
<div class="row">
<div class="col">
<input class="btn btn-secondary" type="button" value="ping" onclick="ping()">
</div>
<div class="col-10" id="pingResult">
</div>
</div>
<div class="row">
<div class="col">
<input class="btn btn-secondary" type="button" value="getBlockHash" onclick="getBlockHash()">
Expand Down

0 comments on commit 4ba0769

Please sign in to comment.