Last active
April 18, 2020 03:29
-
-
Save rraallvv/edc5e0ec5bcba8799f1aec3531568f85 to your computer and use it in GitHub Desktop.
socket.io example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<body> | |
<script src="socket.io.js"></script> | |
<script> | |
eventToListenTo = 'tx' | |
room = 'inv' | |
packages = 0; | |
var socket = io("http://localhost:8000/"); | |
socket.on('connect', function() { | |
console.log('connected!!!'); | |
// Join the room. | |
socket.emit('subscribe', room); | |
}) | |
socket.on(eventToListenTo, function(data) { | |
console.log(data); | |
if (packages > 10) { | |
console.log('unsubscribed!!!'); | |
socket.emit('unsubscribe', room); | |
} else { | |
packages++; | |
} | |
}) | |
</script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const | |
port = 8000, | |
io = require("socket.io"), | |
server = io.listen(port), | |
dummy = { | |
txid: "e3564341b77fe099b1d123bece39057e1cc4cf446376612dc6eaab6957b3f180", | |
valueOut: 123.0, | |
vout: [ | |
{XhayrpwPfELBNjErrBmHe6qpsaiwFSWzyX: 12682308} | |
], | |
isRBF: false, | |
txlock: false | |
}; | |
// event fired every time a new client connects: | |
server.on("connection", (socket) => { | |
console.info(`Client connected [id=${socket.id}]`); | |
socket.on("disconnect", () => { | |
console.info(`Client gone [id=${socket.id}]`); | |
}); | |
socket.on("subscribe", (data) => { | |
if (data === "inv") { | |
socket.join("inv", () => { | |
console.info("Subscribed"); | |
}); | |
} | |
}); | |
socket.on("unsubscribe", (data) => { | |
if (data === "inv") { | |
socket.leave("inv", () => { | |
console.info("Unsubscribed"); | |
}); | |
} | |
}); | |
}); | |
// sends each client its current sequence number | |
setInterval(() => { | |
console.log('.'); | |
server.to('inv').emit('tx', dummy); | |
}, 5000); | |
console.log(`App started on port ${port}`); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// get data for each confirmed transaction | |
$.client.addHeadChangedListener(async (hash, reason) => { | |
const block = await $.client.getBlock(hash); | |
const transactions = block.body.transactions; | |
for (const transaction of transactions) { | |
const data = { | |
txid: transaction.hash().toPlain(), | |
value: transaction.value, | |
sender: transaction.recipient.toUserFriendlyAddress() | |
}; | |
console.log("!!!", data); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment