0

我是比特币、Blockchain.info API 和 javascript 的新手,但是,我正在尝试实现一个代码来跟踪特定比特币地址上的实时支付通知。这里的思路是……用户扫描二维码<img src="http://chart.googleapis.com/chart?chs=200x200&cht=qr&chl=12fMma2J15qre9bZPsX3AerdgWd9Poe9ee">,支付到BTC地址后,12fMma2J15qre9bZPsX3AerdgWd9Poe9ee带有ID的Div#websocket会立即显示Live(无需刷新网页),转入地址的比特币数量,从而切换div 的初始内容从monitoring...转入金额Recieved: 0.003 BTC

我已经写了一段代码......但我不确定我错过了什么。请帮忙。谢谢你。

编码:

        <div class="row">
            <div class="col-md-4 "> 
                <img src="http://chart.googleapis.com/chart?chs=200x200&cht=qr&chl=12fMma2J15qre9bZPsX3AerdgWd9Poe9ee"> 
                <div id="websocket">
                    Monitoring Transactions ...
                </div>

                <script>
                var btcs = new WebSocket("12fMma2J15qre9bZPsX3AerdgWd9Poe9ee");
                btcs.onopen = function() {
                    btcs.send(JSON.stringify({"op":"addr_sub", "addr":"12fMma2J15qre9bZPsX3AerdgWd9Poe9ee"}));
                };

                btcs.onmessage = function (onmsg) {
                    var response = JSON.parse(onmsg.data);
                    var getOutputs = response.x.out;
                    var countOuts = getOutputs.length;

                    for (i=0; i < countOuts; i++){
                        var outAdd = response.x.out[i].addr;
                        var address = "12fMma2J15qre9bZPsX3AerdgWd9Poe9ee";
                        if (outAdd == address){
                            var amount =response.x.out[i].value;
                            var calAmount = Amount / 100000000;
                            document.getElementById("websocket").innerHTML = "Recieved" + calAmount + "BTC";
                        }
                    }
                };
                </script>

            </div>
            <div class="col-md-8">
                <!-- more html stuff goes here -->
            </div>
        </div>
4

3 回答 3

2

我在代码中的任何地方都看不到与区块链的 api 建立连接,所以我猜这个

var btcs = new WebSocket("12fMma2J15qre9bZPsX3AerdgWd9Poe9ee");

应该使用 api 的地址而不是目标比特币钱包地址。

于 2017-12-28T02:30:04.710 回答
1

可能您需要wss://ws.blockchain.info/inv在地址位置启动新套接字时放置。

于 2017-12-18T18:39:47.243 回答
1
    <div class="row">
        <div class="col-md-4 "> 
            <img src="http://chart.googleapis.com/chart?chs=200x200&cht=qr&chl=12fMma2J15qre9bZPsX3AerdgWd9Poe9ee"> 
            <div id="websocket">
                Monitoring Transactions ...
            </div>

            <script>
            var btcs = new WebSocket("wss://ws.blockchain.info/inv");
            btcs.onopen = function() {
                btcs.send(JSON.stringify({"op":"addr_sub", "addr":"12fMma2J15qre9bZPsX3AerdgWd9Poe9ee"}));
            };

            btcs.onmessage = function (onmsg) {
                var response = JSON.parse(onmsg.data);
                var getOutputs = response.x.out;
                var countOuts = getOutputs.length;

                for (i=0; i < countOuts; i++){
                    var outAdd = response.x.out[i].addr;
                    var address = "12fMma2J15qre9bZPsX3AerdgWd9Poe9ee";
                    if (outAdd == address){
                        var amount =response.x.out[i].value;
                        var calAmount = Amount / 100000000;
                        document.getElementById("websocket").innerHTML = "Recieved" + calAmount + "BTC";
                    }
                }
            };
            </script>

        </div>
        <div class="col-md-8">
            <!-- more html stuff goes here -->
        </div>
    </div>
于 2019-03-08T04:36:24.760 回答