chore(project) rename main directory
This commit is contained in:
82
lib/WetSock/Client/Client.js
Normal file
82
lib/WetSock/Client/Client.js
Normal file
@@ -0,0 +1,82 @@
|
||||
export default class WetSockClient {
|
||||
constructor(url) {
|
||||
this.socket = new WebSocket(url);
|
||||
this.listeners = new Map();
|
||||
this.plugins = new Map();
|
||||
|
||||
this.socket.onopen = () => {
|
||||
console.log('Connected to WetSock server');
|
||||
};
|
||||
|
||||
this.socket.onmessage = (event) => {
|
||||
const message = JSON.parse(event.data);
|
||||
this.handleMessage(message);
|
||||
};
|
||||
|
||||
this.socket.onclose = () => {
|
||||
console.log('Disconnected from WetSock server');
|
||||
};
|
||||
}
|
||||
|
||||
on(event, callback) {
|
||||
if (!this.listeners.has(event)) {
|
||||
this.listeners.set(event, []);
|
||||
}
|
||||
this.listeners.get(event).push(callback);
|
||||
}
|
||||
|
||||
emit(event, ...args) {
|
||||
const listeners = this.listeners.get(event);
|
||||
if (listeners) {
|
||||
listeners.forEach((listener) => listener(...args));
|
||||
}
|
||||
}
|
||||
|
||||
handleMessage(message) {
|
||||
const { event, ...data } = message;
|
||||
|
||||
if (event === 'pluginList') {
|
||||
this.updatePlugins(data.plugins);
|
||||
} else {
|
||||
this.emit(event, data);
|
||||
}
|
||||
}
|
||||
|
||||
updatePlugins(plugins) {
|
||||
plugins.forEach((plugin) => {
|
||||
this.plugins.set(plugin.identifier, plugin.actions);
|
||||
});
|
||||
}
|
||||
|
||||
send(message) {
|
||||
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 ...
|
||||
}
|
||||
3
lib/WetSock/Client/index.js
Normal file
3
lib/WetSock/Client/index.js
Normal file
@@ -0,0 +1,3 @@
|
||||
import WetSockClient from './WetSockClient';
|
||||
|
||||
export default WetSockClient;
|
||||
24
lib/WetSock/Models/Repository.js
Normal file
24
lib/WetSock/Models/Repository.js
Normal file
@@ -0,0 +1,24 @@
|
||||
export default class Repository {
|
||||
#entityClass = undefined;
|
||||
#actions = new Map();
|
||||
constructor(config) {
|
||||
this.#entityClass = config.entityClass;
|
||||
}
|
||||
|
||||
create(entityData) {
|
||||
return new this.#entityClass(entityData);
|
||||
}
|
||||
|
||||
perform(entity, actionName, ...args) {
|
||||
if(!this.#actions.has(actionName)) throw new Error(`Action '${actionName}' is not registered.`);
|
||||
return this.#actions.get(actionName)(entity, ...args);
|
||||
}
|
||||
|
||||
register(actionName, actionFn) {
|
||||
this.#actions.set(actionName, actionFn);
|
||||
}
|
||||
|
||||
getActions() {
|
||||
return Array.from(this.#actions.keys());
|
||||
}
|
||||
}
|
||||
19
lib/WetSock/Models/Room.js
Normal file
19
lib/WetSock/Models/Room.js
Normal file
@@ -0,0 +1,19 @@
|
||||
export default class Room {
|
||||
constructor(data) {
|
||||
this.id = data.id;
|
||||
this.name = data.name;
|
||||
this.users = new Set();
|
||||
}
|
||||
|
||||
addUser(userId) {
|
||||
this.users.add(userId);
|
||||
}
|
||||
|
||||
removeUser(userId) {
|
||||
this.users.delete(userId);
|
||||
}
|
||||
|
||||
getUsers() {
|
||||
return Array.from(this.users);
|
||||
}
|
||||
}
|
||||
13
lib/WetSock/Plugins/BasePlugin.js
Normal file
13
lib/WetSock/Plugins/BasePlugin.js
Normal file
@@ -0,0 +1,13 @@
|
||||
export default class BasePlugin {
|
||||
#wetsock = undefined;
|
||||
constructor() {
|
||||
}
|
||||
|
||||
initialize(wetsock) {
|
||||
this.#wetsock = wetsock;
|
||||
}
|
||||
|
||||
onMessage(clientId, message) {}
|
||||
onOpen(clientId) {}
|
||||
onClose(clientId) {}
|
||||
}
|
||||
65
lib/WetSock/Plugins/EntityPlugin.js
Normal file
65
lib/WetSock/Plugins/EntityPlugin.js
Normal file
@@ -0,0 +1,65 @@
|
||||
import BasePlugin from './BasePlugin';
|
||||
|
||||
class EntityPlugin extends BasePlugin {
|
||||
#config = {};
|
||||
#entities = new Map();
|
||||
#repository = null;
|
||||
|
||||
constructor(config) {
|
||||
super();
|
||||
this.#config = config;
|
||||
}
|
||||
|
||||
initialize(wetsock) {
|
||||
super.initialize(wetsock);
|
||||
|
||||
this.#repository = this.#config.repository;
|
||||
this.#config.entities.forEach((data) => {
|
||||
const entity = this.#repository.create(data);
|
||||
this.#entities.set(entity.id, entity);
|
||||
});
|
||||
}
|
||||
|
||||
onMessage(clientId, message) {
|
||||
const { action, entityId, data } = message;
|
||||
|
||||
if (action === 'create') {
|
||||
const entity = this.#repository.create(data);
|
||||
this.#entities.set(entity.id, entity);
|
||||
this.broadcast(clientId, { event: 'entityCreated', entityId: entity.id });
|
||||
} else if (action === 'perform') {
|
||||
const entity = this.#entities.get(entityId);
|
||||
if (entity) {
|
||||
const result = this.#repository.perform(entity, data.actionName, ...data.args);
|
||||
this.broadcast(clientId, { event: 'actionPerformed', entityId, actionName: data.actionName, result });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
onOpen(clientId) {
|
||||
// Send the list of available entities to the client
|
||||
const entityList = Array.from(this.#entities.values());
|
||||
this.send(clientId, { event: 'entityList', entities: entityList });
|
||||
}
|
||||
|
||||
onClose(clientId) {
|
||||
// Handle client disconnection if needed
|
||||
}
|
||||
|
||||
broadcast(clientId, message) {
|
||||
// Broadcast the message to all connected clients
|
||||
for (const [id, client] of this.#wetsock.clients) {
|
||||
if (id !== clientId) {
|
||||
client.send(JSON.stringify(message));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
send(clientId, message) {
|
||||
// Send the message to a specific client
|
||||
const client = this.#wetsock.clients.get(clientId);
|
||||
if (client) {
|
||||
client.send(JSON.stringify(message));
|
||||
}
|
||||
}
|
||||
}
|
||||
54
lib/WetSock/WetSock.js
Normal file
54
lib/WetSock/WetSock.js
Normal file
@@ -0,0 +1,54 @@
|
||||
export default class WetSock {
|
||||
#config = undefined;
|
||||
#server = undefined;
|
||||
#clients = new Map();
|
||||
#plugins = new Map();
|
||||
constructor(config) {
|
||||
this.config = config;
|
||||
this.server = null;
|
||||
}
|
||||
|
||||
start(server) {
|
||||
this.#server = server;
|
||||
}
|
||||
|
||||
registerPlugin(identifier, plugin) {
|
||||
plugin.initialize(this);
|
||||
this.#plugins.set(identifier, plugin);
|
||||
}
|
||||
|
||||
getPlugin(identifier) {
|
||||
return this.#plugins.get(identifier);
|
||||
}
|
||||
|
||||
handleUpgrade(req) {
|
||||
const clientId = Math.random().toString(36).substring(2);
|
||||
const success = this.#server.upgrade(req, { data: { clientId } });
|
||||
if (success) {
|
||||
return undefined;
|
||||
}
|
||||
return Response.redirect('/', 302);
|
||||
}
|
||||
|
||||
handleMessage(ws, message) {
|
||||
const client = this.#clients.get(ws.data.clientId);
|
||||
for(const plugin of this.#plugins) {
|
||||
if(plugin.onMessage(ws.data.clientId, message)) break; // plugin consumes message by returning a truthy value
|
||||
}
|
||||
}
|
||||
|
||||
handleOpen(ws) {
|
||||
const client = this.#clients.get(ws.data.clientId);
|
||||
this.#plugins.forEach((plugin) => plugin.onOpen(ws.data.clientId));
|
||||
}
|
||||
|
||||
handleClose(ws) {
|
||||
const client = this.#clients.get(ws.data.clientId);
|
||||
this.#plugins.forEach((plugin) => plugin.onClose(ws.data.clientId));
|
||||
this.#clients.delete(ws.data.clientId);
|
||||
}
|
||||
|
||||
handleDrain(ws) {
|
||||
// Handle socket drain event
|
||||
}
|
||||
}
|
||||
3
lib/WetSock/index.js
Normal file
3
lib/WetSock/index.js
Normal file
@@ -0,0 +1,3 @@
|
||||
import WetSock from './WetSock';
|
||||
|
||||
export default WetSock;
|
||||
Reference in New Issue
Block a user