From cf279d4a3e540d84d508f0722965305d9930b797 Mon Sep 17 00:00:00 2001 From: gh0sTedBuddy Date: Sun, 7 Apr 2024 20:08:40 +0100 Subject: [PATCH] chore(example) introduce changes to example implementation --- lib/WetSock/Client/Client.js | 62 +++++++++++++----------------------- lib/index.js | 20 +++++++----- 2 files changed, 34 insertions(+), 48 deletions(-) diff --git a/lib/WetSock/Client/Client.js b/lib/WetSock/Client/Client.js index a05a894..d1025da 100644 --- a/lib/WetSock/Client/Client.js +++ b/lib/WetSock/Client/Client.js @@ -4,18 +4,18 @@ export default class WetSockClient { this.listeners = new Map(); this.plugins = new Map(); - this.socket.onopen = () => { + this.socket.addEventListener('open', () => { console.log('Connected to WetSock server'); - }; + }); - this.socket.onmessage = (event) => { + this.socket.addEventListener('message', (event) => { const message = JSON.parse(event.data); this.handleMessage(message); - }; + }); - this.socket.onclose = () => { + this.socket.addEventListener('close', () => { console.log('Disconnected from WetSock server'); - }; + }); } on(event, callback) { @@ -33,13 +33,14 @@ export default class WetSockClient { } handleMessage(message) { - const { event, ...data } = message; - - if (event === 'pluginList') { - this.updatePlugins(data.plugins); - } else { - this.emit(event, data); + this.emit('message', message); + const { event, data, ...rest } = message; + switch (event) { + case 'plugins': + this.updatePlugins(data); + break; } + this.emit(event, data); } updatePlugins(plugins) { @@ -49,34 +50,15 @@ export default class WetSockClient { } send(message) { + if(typeof(message) != 'string') throw new TypeError('message needs to be a string'); + if(message.startsWith('/')) { + const [command, ...rest] = message.split(' '); + + message = { + action: command.substring(1), + message: rest.join(' ') + } + } this.socket.send(JSON.stringify(message)); } - - // Generic methods for interacting with plugins - createEntity(pluginIdentifier, entityData) { - this.send({ action: 'create', pluginIdentifier, data: entityData }); - } - - performAction(pluginIdentifier, entityId, actionName, ...args) { - this.send({ action: 'perform', pluginIdentifier, entityId, data: { actionName, args } }); - } - - // Helper methods for common actions - joinRoom(roomId) { - this.performAction('room', roomId, 'join'); - } - - leaveRoom(roomId) { - this.performAction('room', roomId, 'leave'); - } - - sendMessageToRoom(roomId, message) { - this.performAction('room', roomId, 'sendMessage', message); - } - - createTask(taskData) { - this.createEntity('task', taskData); - } - - // ... other helper methods ... } diff --git a/lib/index.js b/lib/index.js index d5d8b4b..0e7f97c 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,24 +1,28 @@ import { resolve } from 'path'; import WetSock from './WetSock'; - - +import RoomPlugin from './WetSock/Plugins/Rooms/RoomPlugin'; const wetsock = new WetSock(); +const rooms = new RoomPlugin(); + +wetsock.registerPlugin('rooms', rooms); + const server = Bun.serve({ - port: process.env.APP_PORT || 43_069, - fetch(req, server) { + port: process.env.APP_PORT || 3_000, + async fetch(req, server) { const url = new URL(req.url); if(url.pathname == '/chat') return wetsock.handleUpgrade(req, server); + let filePath = resolve(__dirname, 'WetSock', 'Client', 'Assets', 'index.html'); if(url.pathname == '/client.js') { - const filePath = resolve(__dirname, 'WetSock', 'Client', 'Client.js'); - const file = Bun.file(filePath); - return new Response(file); + filePath = resolve(__dirname, 'WetSock', 'Client', 'Client.js'); } - const filePath = resolve(__dirname, 'views', 'index.html'); const file = Bun.file(filePath); + if(!(await file.exists())) { + return new Response('Not found', {status: 404}); + } return new Response(file); }, websocket: {