initial merge of both repos
This commit is contained in:
69
UNO-Backend/server.ts
Normal file
69
UNO-Backend/server.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
import { UnoSockets } from './src/gameLogic/UnoSocket.js'
|
||||
import express from "express";
|
||||
import bodyParser from 'body-parser'
|
||||
import { UnoSession} from './src/gameLogic/UnoSession.js'
|
||||
import expressWs from 'express-ws';
|
||||
import http from 'http';
|
||||
import WebSocket from 'ws';
|
||||
import { UnoLobby } from './src/lobby/UnoLobby.js';
|
||||
import { resp } from './src/interface/rest/response.js';
|
||||
import { watch } from 'fs';
|
||||
|
||||
|
||||
const app = express();
|
||||
app.use(bodyParser.json())
|
||||
|
||||
|
||||
let unoLobby = new UnoLobby();
|
||||
|
||||
|
||||
app.get('/', (req, res) => res.send('Hello World!'));
|
||||
|
||||
|
||||
//lobby requests
|
||||
app.get('/unogame/lobbies', (req,res)=>{
|
||||
resp(req, res, true, unoLobby.getLobbies(), 200);
|
||||
})
|
||||
|
||||
//lobby requests
|
||||
app.post('/unogame/create', (req,res)=>{
|
||||
resp(req, res, true, {
|
||||
id: unoLobby.createLobby(req.body),
|
||||
}, 200);
|
||||
})
|
||||
|
||||
//lobby join
|
||||
app.post('/unogame/join', (req,res)=>{
|
||||
let data = unoLobby.joinLobby(req.body.nick,req.body.lid);
|
||||
resp(req, res, data[0], data[1], 200);
|
||||
})
|
||||
|
||||
|
||||
|
||||
const port = 3000;
|
||||
const server = app.listen(port, () => console.log(`Example app listening on port ${port}!`));
|
||||
|
||||
|
||||
|
||||
|
||||
const wss = new WebSocket.Server({ server });
|
||||
|
||||
wss.on('connection', (ws: WebSocket) => {
|
||||
console.log("connecting socket...")
|
||||
ws.on('message', (message: string) => { actionHandler(message,ws);});
|
||||
ws.send('{"msg":"Hello!"}');
|
||||
});
|
||||
|
||||
|
||||
function actionHandler(message: string, ws: WebSocket){
|
||||
try{
|
||||
const command = JSON.parse(message);
|
||||
console.log(command);
|
||||
if (command.action == "register"){
|
||||
unoLobby.registerLobby(message,ws)
|
||||
}
|
||||
}catch(ex){
|
||||
ws.send('{"reply":"Error"}');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user