30 lines
914 B
TypeScript
30 lines
914 B
TypeScript
const TRIGGER_CHAR = {
|
|
PING: String.fromCharCode(0x02),
|
|
PONG: String.fromCharCode(0x03),
|
|
STATUS: String.fromCharCode(0x04),
|
|
};
|
|
|
|
export default defineWebSocketHandler({
|
|
message(peer, message) {
|
|
switch(message.rawData)
|
|
{
|
|
case TRIGGER_CHAR.PING:
|
|
peer.send(TRIGGER_CHAR.PONG);
|
|
return;
|
|
default:
|
|
return;
|
|
}
|
|
},
|
|
open(peer) {
|
|
const id = new URL(peer.request.url).pathname.split('/').slice(-1)[0];
|
|
if(!id) return peer.close();
|
|
peer.subscribe(`campaigns/${id}`);
|
|
peer.publish(`campaigns/${id}`, `${TRIGGER_CHAR.STATUS}`);
|
|
},
|
|
close(peer, details) {
|
|
const id = new URL(peer.request.url).pathname.split('/').slice(-1)[0];
|
|
if(!id) return peer.close();
|
|
peer.publish(`campaigns/${id}`, false);
|
|
peer.unsubscribe(`campaigns/${id}`);
|
|
}
|
|
}); |