简体   繁体   中英

Javascript / Blockchain.info Websocket API Address Subscription

I am new to bitcoin, Blockchain.info API and javascript, however, i am trying to implement a code that tracks Live Payments notification on a particular bitcoin address. The idea here is... after the user scans the QR image <img src="http://chart.googleapis.com/chart?chs=200x200&cht=qr&chl=12fMma2J15qre9bZPsX3AerdgWd9Poe9ee"> , and makes payment to the BTC address, 12fMma2J15qre9bZPsX3AerdgWd9Poe9ee , the Div with ID #websocket will instantly display Live (without refreshing the webpage), the amount of Bitcoins Transaferred to the address, thus switching the initial content of the div from monitoring... to the amount transferred Recieved: 0.003 BTC .

I have written a piece of code ... but i'm not sure what i'm missing. Please Help. Thank you.

The code:

        <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>

I don't see anywhere in the code a connection being made to blockchain's api, so I'm guessing this

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

should use the api's address instead of the target bitcoin wallet address.

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

    <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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM