node-ws-packets
Version:
A packet-based javascript websocket communitcation.
40 lines (35 loc) • 962 B
HTML
<script src="./Browser.js"></script>
<script>
const ws = new WebSocket("ws://localhost:8080");
const cm = new Client(ws);
cm.onConnect((ws) => {
console.log("client", "I have connected!");
ws.sendPacket(new Ping({timestamp: 123}))
});
cm.onDisconnect((ws) => {
console.log("client", "I have disconnected!");
});
class Ping extends Packet {
constructor(payload) {
/*
Create Model
Used to check data types
*/
const model = {
timestamp: Number
}
/*
Init Packet
*/
super("Ping", payload, model);
}
/**
* Handle packet
* @param {object} ws websocket connection
*/
handle(ws) {
console.log(this);
}
}
cm.addPacket(new Ping());
</script>