app.js

const express = require('express'); const app = express(); const WebSocket = require("ws"); app.get('/', function (req, res) { res.send('This is not really a web page.'); }); const port = 3997 const wsPort = 3998 app.listen(3997, function () { console.log(`Example app listening on port ${port}!`); let receivedEvents = 0; const ids = [ "0x60674ffbef620598f735d6c9c120b422c48830a6", "0x60674ffbef620598f735d6c9c120b422c48830a6", "0x4c9178b95977de41690c6067a771388147b3a70b" ] try { const ws = new WebSocket(`ws://localhost:${wsPort}`) .on("message", (msg) => { const decoded = JSON.parse(msg); switch (decoded.type) { case "connected": console.log("connected") ws.send(JSON.stringify({ ids: ids })); break; case "subscribed": console.log("Subscribed") break; case "data": case "changed": receivedEvents++; console.log("new event: ", decoded); break; default: ws.close(); } }) .on("ping", () => { console.log("got ping"); }) .on("close", () => { console.log("woohoo!"); }); } catch (e) { console.log("error: ", e) } });
Toy websocket client

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.