我是Blockchain的新手,我想了解如何使用JS打开Websocket,代码来自Blockcypher.com docs https://www.blockcypher.com/dev/dash/?javascript#using-websockets
// Get latest unconfirmed transactions live
var ws = new WebSocket("wss://socket.blockcypher.com/v1/dash/main");
var count = 0;
ws.onmessage = function (event) {
var tx = JSON.parse(event.data);
var shortHash = tx.hash.substring(0, 6) + "...";
var total = tx.total / 100000000;
var addrs = tx.addresses.join(", ");
$('#browser-websocket').before("<div>Unconfirmed transaction " + shortHash + " totalling " + total + "DASH involving addresses " + addrs + "</div>");
count++;
if (count > 10) ws.close();
}
ws.onopen = function(event) {
ws.send(JSON.stringify({event: "unconfirmed-tx"}));
}浏览器控制台显示此错误:Firefox can’t establish a connection to the server at wss://socket.blockcypher.com/v1/dash/main.

注意:我有一个由提供的令牌,但是它们提供的代码示例并不要求任何令牌。密码坏吗?是不是漏掉了什么?
发布于 2021-06-19 11:08:54
问题不在您的代码中,而是服务器没有按预期响应。
这可能是服务器问题,或者您可能需要预先批准才能访问该资源(假设它们正在白名单上显示IP地址,我对此表示怀疑)。
另外,查看代码的其余部分,它有一个占位符,用于您可能需要连接的令牌(您现在看到的错误是在请求令牌之前)。
您始终可以将服务器名称更改为wss:// error .websocket.org等众多测试服务器之一,并看到错误从“无法建立连接”更改为与代码其余部分相关的内容。
https://stackoverflow.com/questions/68045802
复制相似问题