2024-04-02 15:57:41 +00:00
|
|
|
import { resolve } from 'path';
|
|
|
|
|
|
2024-04-02 23:30:37 +00:00
|
|
|
import WetSock from './WetSock';
|
2024-04-07 20:08:40 +01:00
|
|
|
import RoomPlugin from './WetSock/Plugins/Rooms/RoomPlugin';
|
|
|
|
|
const wetsock = new WetSock();
|
2024-04-02 23:30:37 +00:00
|
|
|
|
2024-04-07 20:08:40 +01:00
|
|
|
const rooms = new RoomPlugin();
|
2024-04-02 23:30:37 +00:00
|
|
|
|
2024-04-07 20:08:40 +01:00
|
|
|
wetsock.registerPlugin('rooms', rooms);
|
2024-04-02 15:57:41 +00:00
|
|
|
|
|
|
|
|
const server = Bun.serve({
|
2024-04-07 20:08:40 +01:00
|
|
|
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);
|
2024-04-02 23:22:46 +00:00
|
|
|
if(url.pathname == '/chat') return wetsock.handleUpgrade(req, server);
|
2024-04-02 15:57:41 +00:00
|
|
|
|
2024-04-07 20:08:40 +01:00
|
|
|
let filePath = resolve(__dirname, 'WetSock', 'Client', 'Assets', 'index.html');
|
2024-04-02 15:57:41 +00:00
|
|
|
if(url.pathname == '/client.js') {
|
2024-04-07 20:08:40 +01:00
|
|
|
filePath = resolve(__dirname, 'WetSock', 'Client', 'Client.js');
|
2024-04-02 15:57:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const file = Bun.file(filePath);
|
2024-04-07 20:08:40 +01:00
|
|
|
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);
|