You've already forked obsidian-visualiser
WebSocket API, new ID/encrypt/decrypt algorithm.
This commit is contained in:
@@ -1,30 +1,36 @@
|
||||
const TRIGGER_CHAR = {
|
||||
PING: String.fromCharCode(0x02),
|
||||
PONG: String.fromCharCode(0x03),
|
||||
STATUS: String.fromCharCode(0x04),
|
||||
};
|
||||
import type { SocketMessage } from "#shared/websocket.util";
|
||||
import type { User } from "~/types/auth";
|
||||
|
||||
export default defineWebSocketHandler({
|
||||
message(peer, message) {
|
||||
switch(message.rawData)
|
||||
const data = message.json<SocketMessage>();
|
||||
switch(data.type)
|
||||
{
|
||||
case TRIGGER_CHAR.PING:
|
||||
peer.send(TRIGGER_CHAR.PONG);
|
||||
return;
|
||||
default:
|
||||
case 'PING':
|
||||
peer.send(JSON.stringify({ type: 'PONG' }));
|
||||
return;
|
||||
|
||||
default: return;
|
||||
}
|
||||
},
|
||||
open(peer) {
|
||||
async 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}`);
|
||||
|
||||
const session = await getUserSession(peer);
|
||||
if(!session ||!session.user) return peer.close();
|
||||
peer.context.user = session.user;
|
||||
|
||||
const topic = `campaigns/${id}`;
|
||||
peer.subscribe(topic);
|
||||
peer.publish(topic, { type: 'user', data: [{ user: (peer.context.user as User).id, status: true }] });
|
||||
peer.send({ type: 'user', data: peer.peers.values().filter(e => e.topics.has(topic)).map(e => ({ user: (e.context.user as User).id, status: true })).toArray() })
|
||||
},
|
||||
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.publish(`campaigns/${id}`, { type: 'user', data: [{ user: (peer.context.user as User).id, status: false }] });
|
||||
peer.unsubscribe(`campaigns/${id}`);
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user