This repository has been archived on 2025-03-10. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
wetsock/lib/index.js

37 lines
1.0 KiB
JavaScript
Raw Normal View History

2024-04-02 15:57:41 +00:00
import { resolve } from 'path';
2024-04-02 23:30:37 +00:00
import WetSock from './WetSock';
import RoomPlugin from './WetSock/Plugins/Rooms/RoomPlugin';
const wetsock = new WetSock();
2024-04-02 23:30:37 +00:00
const rooms = new RoomPlugin();
2024-04-02 23:30:37 +00:00
wetsock.registerPlugin('rooms', rooms);
2024-04-02 15:57:41 +00:00
const server = Bun.serve({
port: process.env.APP_PORT || 3_000,
async fetch(req, server) {
2024-04-02 15:57:41 +00:00
const url = new URL(req.url);
if(url.pathname == '/chat') return wetsock.handleUpgrade(req, server);
2024-04-02 15:57:41 +00:00
let filePath = resolve(__dirname, 'WetSock', 'Client', 'Assets', 'index.html');
2024-04-02 15:57:41 +00:00
if(url.pathname == '/client.js') {
filePath = resolve(__dirname, 'WetSock', 'Client', 'Client.js');
2024-04-02 15:57:41 +00:00
}
const file = Bun.file(filePath);
if(!(await file.exists())) {
return new Response('Not found', {status: 404});
}
2024-04-02 15:57:41 +00:00
return new Response(file);
},
websocket: {
open: wetsock.handleOpen.bind(wetsock),
close: wetsock.handleClose.bind(wetsock),
message: wetsock.handleMessage.bind(wetsock),
drain: wetsock.handleDrain.bind(wetsock),
}
});
wetsock.start(server);