2017-03-19 70 views
0

我是比特幣,Blockchain.info API和JavaScript的新手,但是,我試圖實現跟蹤特定比特幣地址上的Live Payments通知的代碼。這裏的想法是......在用戶掃描QR圖像<img src="http://chart.googleapis.com/chart?chs=200x200&cht=qr&chl=12fMma2J15qre9bZPsX3AerdgWd9Poe9ee">並且支付到BTC地址12fMma2J15qre9bZPsX3AerdgWd9Poe9ee後,具有ID #websocket的Div將立即顯示Live(不刷新網頁),將比特幣的數量轉移到地址,從而將div的初始內容從monitoring...切換到轉移金額Recieved: 0.003 BTCJavascript/Blockchain.info Websocket API地址訂閱

我寫了一段代碼......但我不確定我錯過了什麼。請幫忙。謝謝。

代碼:

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

回答

1

也許你需要把wss://ws.blockchain.info/inv而起始地址的地方一個新的套接字。

0

我沒有看到一個連接正在取得blockchain的API代碼的任何地方,所以我猜這個

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

應該使用來代替目標比特幣錢包地址API的地址。