tcp sockets in client not working, need refactor to web sockets :(

This commit is contained in:
JasterV 2020-11-09 00:43:07 +01:00
commit 763a3c7a0d
12 changed files with 683 additions and 96 deletions

366
client/package-lock.json generated
View file

@ -4,6 +4,194 @@
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"accepts": {
"version": "1.3.7",
"resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.7.tgz",
"integrity": "sha512-Il80Qs2WjYlJIBNzNkK6KYqlVMTbZLXgHx2oT0pU/fjRHyEp+PEfEPY0R3WCwAGVOtauxh1hOxNgIf5bv7dQpA==",
"requires": {
"mime-types": "~2.1.24",
"negotiator": "0.6.2"
}
},
"array-flatten": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz",
"integrity": "sha1-ml9pkFGx5wczKPKgCJaLZOopVdI="
},
"body-parser": {
"version": "1.19.0",
"resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.19.0.tgz",
"integrity": "sha512-dhEPs72UPbDnAQJ9ZKMNTP6ptJaionhP5cBb541nXPlW60Jepo9RV/a4fX4XWW9CuFNK22krhrj1+rgzifNCsw==",
"requires": {
"bytes": "3.1.0",
"content-type": "~1.0.4",
"debug": "2.6.9",
"depd": "~1.1.2",
"http-errors": "1.7.2",
"iconv-lite": "0.4.24",
"on-finished": "~2.3.0",
"qs": "6.7.0",
"raw-body": "2.4.0",
"type-is": "~1.6.17"
}
},
"bytes": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.0.tgz",
"integrity": "sha512-zauLjrfCG+xvoyaqLoV8bLVXXNGC4JqlxFCutSDWA6fJrTo2ZuvLYTqZ7aHBLZSMOopbzwv8f+wZcVzfVTI2Dg=="
},
"content-disposition": {
"version": "0.5.3",
"resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.3.tgz",
"integrity": "sha512-ExO0774ikEObIAEV9kDo50o+79VCUdEB6n6lzKgGwupcVeRlhrj3qGAfwq8G6uBJjkqLrhT0qEYFcWng8z1z0g==",
"requires": {
"safe-buffer": "5.1.2"
}
},
"content-type": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/content-type/-/content-type-1.0.4.tgz",
"integrity": "sha512-hIP3EEPs8tB9AT1L+NUqtwOAps4mk2Zob89MWXMHjHWg9milF/j4osnnQLXBCBFBk/tvIG/tUc9mOUJiPBhPXA=="
},
"cookie": {
"version": "0.4.0",
"resolved": "https://registry.npmjs.org/cookie/-/cookie-0.4.0.tgz",
"integrity": "sha512-+Hp8fLp57wnUSt0tY0tHEXh4voZRDnoIrZPqlo3DPiI4y9lwg/jqx+1Om94/W6ZaPDOUbnjOt/99w66zk+l1Xg=="
},
"cookie-signature": {
"version": "1.0.6",
"resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz",
"integrity": "sha1-4wOogrNCzD7oylE6eZmXNNqzriw="
},
"debug": {
"version": "2.6.9",
"resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
"integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==",
"requires": {
"ms": "2.0.0"
}
},
"depd": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/depd/-/depd-1.1.2.tgz",
"integrity": "sha1-m81S4UwJd2PnSbJ0xDRu0uVgtak="
},
"destroy": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/destroy/-/destroy-1.0.4.tgz",
"integrity": "sha1-l4hXRCxEdJ5CBmE+N5RiBYJqvYA="
},
"ee-first": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz",
"integrity": "sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0="
},
"encodeurl": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/encodeurl/-/encodeurl-1.0.2.tgz",
"integrity": "sha1-rT/0yG7C0CkyL1oCw6mmBslbP1k="
},
"escape-html": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/escape-html/-/escape-html-1.0.3.tgz",
"integrity": "sha1-Aljq5NPQwJdN4cFpGI7wBR0dGYg="
},
"etag": {
"version": "1.8.1",
"resolved": "https://registry.npmjs.org/etag/-/etag-1.8.1.tgz",
"integrity": "sha1-Qa4u62XvpiJorr/qg6x9eSmbCIc="
},
"express": {
"version": "4.17.1",
"resolved": "https://registry.npmjs.org/express/-/express-4.17.1.tgz",
"integrity": "sha512-mHJ9O79RqluphRrcw2X/GTh3k9tVv8YcoyY4Kkh4WDMUYKRZUq0h1o0w2rrrxBqM7VoeUVqgb27xlEMXTnYt4g==",
"requires": {
"accepts": "~1.3.7",
"array-flatten": "1.1.1",
"body-parser": "1.19.0",
"content-disposition": "0.5.3",
"content-type": "~1.0.4",
"cookie": "0.4.0",
"cookie-signature": "1.0.6",
"debug": "2.6.9",
"depd": "~1.1.2",
"encodeurl": "~1.0.2",
"escape-html": "~1.0.3",
"etag": "~1.8.1",
"finalhandler": "~1.1.2",
"fresh": "0.5.2",
"merge-descriptors": "1.0.1",
"methods": "~1.1.2",
"on-finished": "~2.3.0",
"parseurl": "~1.3.3",
"path-to-regexp": "0.1.7",
"proxy-addr": "~2.0.5",
"qs": "6.7.0",
"range-parser": "~1.2.1",
"safe-buffer": "5.1.2",
"send": "0.17.1",
"serve-static": "1.14.1",
"setprototypeof": "1.1.1",
"statuses": "~1.5.0",
"type-is": "~1.6.18",
"utils-merge": "1.0.1",
"vary": "~1.1.2"
}
},
"finalhandler": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-1.1.2.tgz",
"integrity": "sha512-aAWcW57uxVNrQZqFXjITpW3sIUQmHGG3qSb9mUah9MgMC4NeWhNOlNjXEYq3HjRAvL6arUviZGGJsBg6z0zsWA==",
"requires": {
"debug": "2.6.9",
"encodeurl": "~1.0.2",
"escape-html": "~1.0.3",
"on-finished": "~2.3.0",
"parseurl": "~1.3.3",
"statuses": "~1.5.0",
"unpipe": "~1.0.0"
}
},
"forwarded": {
"version": "0.1.2",
"resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.1.2.tgz",
"integrity": "sha1-mMI9qxF1ZXuMBXPozszZGw/xjIQ="
},
"fresh": {
"version": "0.5.2",
"resolved": "https://registry.npmjs.org/fresh/-/fresh-0.5.2.tgz",
"integrity": "sha1-PYyt2Q2XZWn6g1qx+OSyOhBWBac="
},
"http-errors": {
"version": "1.7.2",
"resolved": "https://registry.npmjs.org/http-errors/-/http-errors-1.7.2.tgz",
"integrity": "sha512-uUQBt3H/cSIVfch6i1EuPNy/YsRSOUBXTVfZ+yR7Zjez3qjBz6i9+i4zjNaoqcoFVI4lQJ5plg63TvGfRSDCRg==",
"requires": {
"depd": "~1.1.2",
"inherits": "2.0.3",
"setprototypeof": "1.1.1",
"statuses": ">= 1.5.0 < 2",
"toidentifier": "1.0.0"
}
},
"iconv-lite": {
"version": "0.4.24",
"resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz",
"integrity": "sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==",
"requires": {
"safer-buffer": ">= 2.1.2 < 3"
}
},
"inherits": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz",
"integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4="
},
"ipaddr.js": {
"version": "1.9.1",
"resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz",
"integrity": "sha512-0KI/607xoxSToH7GjN1FfSbLoU0+btTicjsQSWQlh/hZykN8KpmMf7uYwPW3R+akZ6R/w18ZlXSHBYXiYUPO3g=="
},
"jquery": {
"version": "3.5.1",
"resolved": "https://registry.npmjs.org/jquery/-/jquery-3.5.1.tgz",
@ -14,15 +202,193 @@
"resolved": "https://registry.npmjs.org/main.css/-/main.css-2.1.0.tgz",
"integrity": "sha512-nKb/vy0+0Y/OFVzHWooY6hJlZz2aYLy7le+hky9AQJJQGsc5OU8qA451jvemSb3GuPmIcnNZgfx9wKl5zDEqxQ=="
},
"media-typer": {
"version": "0.3.0",
"resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz",
"integrity": "sha1-hxDXrwqmJvj/+hzgAWhUUmMlV0g="
},
"merge-descriptors": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-1.0.1.tgz",
"integrity": "sha1-sAqqVW3YtEVoFQ7J0blT8/kMu2E="
},
"methods": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/methods/-/methods-1.1.2.tgz",
"integrity": "sha1-VSmk1nZUE07cxSZmVoNbD4Ua/O4="
},
"mime": {
"version": "1.6.0",
"resolved": "https://registry.npmjs.org/mime/-/mime-1.6.0.tgz",
"integrity": "sha512-x0Vn8spI+wuJ1O6S7gnbaQg8Pxh4NNHb7KSINmEWKiPE4RKOplvijn+NkmYmmRgP68mc70j2EbeTFRsrswaQeg=="
},
"mime-db": {
"version": "1.44.0",
"resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.44.0.tgz",
"integrity": "sha512-/NOTfLrsPBVeH7YtFPgsVWveuL+4SjjYxaQ1xtM1KMFj7HdxlBlxeyNLzhyJVx7r4rZGJAZ/6lkKCitSc/Nmpg=="
},
"mime-types": {
"version": "2.1.27",
"resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.27.tgz",
"integrity": "sha512-JIhqnCasI9yD+SsmkquHBxTSEuZdQX5BuQnS2Vc7puQQQ+8yiP5AY5uWhpdv4YL4VM5c6iliiYWPgJ/nJQLp7w==",
"requires": {
"mime-db": "1.44.0"
}
},
"ms": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
"integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g="
},
"negotiator": {
"version": "0.6.2",
"resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.2.tgz",
"integrity": "sha512-hZXc7K2e+PgeI1eDBe/10Ard4ekbfrrqG8Ep+8Jmf4JID2bNg7NvCPOZN+kfF574pFQI7mum2AUqDidoKqcTOw=="
},
"net": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/net/-/net-1.0.2.tgz",
"integrity": "sha1-0XV+yaf7I3HYPPR1XOPifhCCk4g="
},
"on-finished": {
"version": "2.3.0",
"resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.3.0.tgz",
"integrity": "sha1-IPEzZIGwg811M3mSoWlxqi2QaUc=",
"requires": {
"ee-first": "1.1.1"
}
},
"parseurl": {
"version": "1.3.3",
"resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz",
"integrity": "sha512-CiyeOxFT/JZyN5m0z9PfXw4SCBJ6Sygz1Dpl0wqjlhDEGGBP1GnsUVEL0p63hoG1fcj3fHynXi9NYO4nWOL+qQ=="
},
"path-to-regexp": {
"version": "0.1.7",
"resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz",
"integrity": "sha1-32BBeABfUi8V60SQ5yR6G/qmf4w="
},
"proxy-addr": {
"version": "2.0.6",
"resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.6.tgz",
"integrity": "sha512-dh/frvCBVmSsDYzw6n926jv974gddhkFPfiN8hPOi30Wax25QZyZEGveluCgliBnqmuM+UJmBErbAUFIoDbjOw==",
"requires": {
"forwarded": "~0.1.2",
"ipaddr.js": "1.9.1"
}
},
"qs": {
"version": "6.7.0",
"resolved": "https://registry.npmjs.org/qs/-/qs-6.7.0.tgz",
"integrity": "sha512-VCdBRNFTX1fyE7Nb6FYoURo/SPe62QCaAyzJvUjwRaIsc+NePBEniHlvxFmmX56+HZphIGtV0XeCirBtpDrTyQ=="
},
"range-parser": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz",
"integrity": "sha512-Hrgsx+orqoygnmhFbKaHE6c296J+HTAQXoxEF6gNupROmmGJRoyzfG3ccAveqCBrwr/2yxQ5BVd/GTl5agOwSg=="
},
"raw-body": {
"version": "2.4.0",
"resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.4.0.tgz",
"integrity": "sha512-4Oz8DUIwdvoa5qMJelxipzi/iJIi40O5cGV1wNYp5hvZP8ZN0T+jiNkL0QepXs+EsQ9XJ8ipEDoiH70ySUJP3Q==",
"requires": {
"bytes": "3.1.0",
"http-errors": "1.7.2",
"iconv-lite": "0.4.24",
"unpipe": "1.0.0"
}
},
"regenerator-runtime": {
"version": "0.13.7",
"resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.13.7.tgz",
"integrity": "sha512-a54FxoJDIr27pgf7IgeQGxmqUNYrcV338lf/6gH456HZ/PhX+5BcwHXG9ajESmwe6WRO0tAzRUrRmNONWgkrew=="
},
"safe-buffer": {
"version": "5.1.2",
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz",
"integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g=="
},
"safer-buffer": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz",
"integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg=="
},
"send": {
"version": "0.17.1",
"resolved": "https://registry.npmjs.org/send/-/send-0.17.1.tgz",
"integrity": "sha512-BsVKsiGcQMFwT8UxypobUKyv7irCNRHk1T0G680vk88yf6LBByGcZJOTJCrTP2xVN6yI+XjPJcNuE3V4fT9sAg==",
"requires": {
"debug": "2.6.9",
"depd": "~1.1.2",
"destroy": "~1.0.4",
"encodeurl": "~1.0.2",
"escape-html": "~1.0.3",
"etag": "~1.8.1",
"fresh": "0.5.2",
"http-errors": "~1.7.2",
"mime": "1.6.0",
"ms": "2.1.1",
"on-finished": "~2.3.0",
"range-parser": "~1.2.1",
"statuses": "~1.5.0"
},
"dependencies": {
"ms": {
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.1.tgz",
"integrity": "sha512-tgp+dl5cGk28utYktBsrFqA7HKgrhgPsg6Z/EfhWI4gl1Hwq8B/GmY/0oXZ6nF8hDVesS/FpnYaD/kOWhYQvyg=="
}
}
},
"serve-static": {
"version": "1.14.1",
"resolved": "https://registry.npmjs.org/serve-static/-/serve-static-1.14.1.tgz",
"integrity": "sha512-JMrvUwE54emCYWlTI+hGrGv5I8dEwmco/00EvkzIIsR7MqrHonbD9pO2MOfFnpFntl7ecpZs+3mW+XbQZu9QCg==",
"requires": {
"encodeurl": "~1.0.2",
"escape-html": "~1.0.3",
"parseurl": "~1.3.3",
"send": "0.17.1"
}
},
"setprototypeof": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.1.1.tgz",
"integrity": "sha512-JvdAWfbXeIGaZ9cILp38HntZSFSo3mWg6xGcJJsd+d4aRMOqauag1C63dJfDw7OaMYwEbHMOxEZ1lqVRYP2OAw=="
},
"statuses": {
"version": "1.5.0",
"resolved": "https://registry.npmjs.org/statuses/-/statuses-1.5.0.tgz",
"integrity": "sha1-Fhx9rBd2Wf2YEfQ3cfqZOBR4Yow="
},
"toidentifier": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.0.tgz",
"integrity": "sha512-yaOH/Pk/VEhBWWTlhI+qXxDFXlejDGcQipMlyxda9nthulaxLZUNcUqFxokp0vcYnvteJln5FNQDRrxj3YcbVw=="
},
"type-is": {
"version": "1.6.18",
"resolved": "https://registry.npmjs.org/type-is/-/type-is-1.6.18.tgz",
"integrity": "sha512-TkRKr9sUTxEH8MdfuCSP7VizJyzRNMjj2J2do2Jr3Kym598JVdEksuzPQCnlFPW4ky9Q+iA+ma9BGm06XQBy8g==",
"requires": {
"media-typer": "0.3.0",
"mime-types": "~2.1.24"
}
},
"unpipe": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz",
"integrity": "sha1-sr9O6FFKrmFltIF4KdIbLvSZBOw="
},
"utils-merge": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz",
"integrity": "sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM="
},
"vary": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz",
"integrity": "sha1-IpnwLG3tMNSllhsLn3RSShj2NPw="
}
}
}

View file

@ -10,6 +10,7 @@
"author": "Víctor Martínez @JasterV",
"license": "ISC",
"dependencies": {
"express": "^4.17.1",
"jquery": "^3.5.1",
"main.css": "^2.1.0",
"net": "^1.0.2",

View file

@ -14,24 +14,29 @@
<span>Join</span>
</div>
<div id="create-btn" , class="choose-card transition-scale">
<div id="create-btn" class="choose-card transition-scale">
<span>Create</span>
</div>
</section>
<section id="join-section" style="display: none;">
<form id="join-form" class="choose-card">
<label for="room-id"></label>
<input type="text" name="room-id" id="room-id" placeholder="Identifier">
<input type="text" name="join-input" id="join-input" placeholder="Identifier">
<input type="submit" class="transition-scale" value="Join!">
<span id="back-btn">Go Back</span>
</form>
</section>
<section id="chat-room" style="display: none;">
<header class="chat-header"></header>
<main>
<div class="chat-body"></div>
<div class="chat-input"></div>
</main>
<div class="chat">
<header class="chat-header">
<h2 id="room-id"></h2>
</header>
<div class="chat-body">
</div>
<div class="chat-input">
<input type="text" placeholder="Write a message...">
</div>
</div>
</section>
</body>

View file

@ -2,22 +2,37 @@ import '../styles/main.css';
import 'regenerator-runtime/runtime'
import { ROOMS_API } from './config';
import $ from 'jquery';
import net from 'net';
const net = require('net');
$(() => {
const App = {
socket: "",
}
$('#join-form').on('submit', async (e) => {
e.preventDefault();
let id = $('#room-id').val().trim();
let id = $('#join-input').val().trim();
if (id.length > 0) {
try {
await fetchRoomInfo(id);
let info = await fetchRoomInfo(id);
openRoom(info);
$("#chat-room, #join-section").toggle();
} catch (err) {
alert(err.message);
}
}
})
$("#create-btn").on('click', async (e) => {
try {
let info = await createRoom();
openRoom(info);
$("#chat-room, #choose-rom").toggle();
} catch (err) {
alert(err.message);
}
});
$('#join-btn').on('click', () => {
$("#choose-room, #join-section").toggle();
})
@ -25,18 +40,49 @@ $(() => {
$("#back-btn").on('click', () => {
$("#choose-room, #join-section").toggle();
});
$(".chat-input input").on('keyup', (e) => {
let val = $(e.currentTarget).val().trim();
if (val.length > 0 && e.key == 'Enter') {
App.socket.write(val);
$('.chat-body').append($(`<div class="right-bubble msg-bubble">${val}</div>`))
}
})
});
async function createRoom() {
let response = await fetch(ROOMS_API + '/rooms?create');
if (!response.ok) throw new Error(response.statusText);
return response.json();
}
async function fetchRoomInfo(id) {
let response = await fetch(ROOMS_API + '/rooms/' + id);
console.log(response);
if (!response.ok) throw new Error(response.statusText);
return response.json();
}
// const client = new net.Socket();
// connector.connect(PORT, HOST, function () {
// console.log('CONNECTED TO: ' + HOST + ':' + PORT);
// connector.write()
// });
// connector.on('data', function (addr) {
// console.log('DATA: ' + data);
// });
function openRoom(data) {
$('#room-id').text(data.id);
openSocket(data.addr);
}
function openSocket(addr) {
addr = addr.split(':');
console.log(addr);
const socket = new net.Socket();
App.socket = socket;
socket.connect(addr[1], addr[0], function () {
console.log('CONNECTED TO: ' + HOST + ':' + PORT);
});
socket.on('data', function (msg) {
$('.chat-body').append($(`<div class="left-bubble msg-bubble">${msg}</div>`))
});
socket.on('close', function () {
alert("Room closed!");
})
}

125
client/src/styles/chat.css Normal file
View file

@ -0,0 +1,125 @@
#chat-room {
justify-content: center;
align-items: center;
}
.chat {
background-color: white;
height: 90%;
width: 100%;
border-radius: 20px;
overflow: hidden;
display: flex;
flex-direction: column;
max-width: 800px;
}
.chat-header {
width: 100%;
border: none;
height: 70px;
display: flex;
align-items: center;
}
.chat-header h2 {
color: #FF8E9C;
text-align: center;
margin: auto;
}
.chat-body {
flex: 1;
overflow: hidden;
overflow-y: auto;
display: flex;
flex-direction: column;
}
.chat-input {
height: 80px;
display: flex;
}
.chat-input input {
flex: 1;
border-radius: 10px;
width: 100%;
border: none;
background-color: #eb7f91;
color: white;
padding: 1rem;
font-size: 1rem;
font-weight: bold;
}
.msg-bubble {
margin: 1.3rem;
border-radius: 15px;
background: #24e2e2;
color: #fff;
padding: 20px;
text-align: center;
font-weight: bold;
position: relative;
min-width: 200px;
max-width: 300px;
}
.bubble-right {
margin-right: auto;
}
.bubble-left {
margin-left: auto;
background-color: #FF8E9C;
}
.bubble-left:before {
content: "";
width: 0px;
height: 0px;
position: absolute;
border-left: 15px solid #FF8E9C;
border-right: 15px solid transparent;
border-top: 15px solid #FF8E9C;
border-bottom: 15px solid transparent;
right: -16px;
top: 0px;
}
.bubble-right:before {
content: "";
width: 0px;
height: 0px;
position: absolute;
border-left: 15px solid transparent;
border-right: 15px solid #24e2e2;
border-top: 15px solid #24e2e2;
border-bottom: 15px solid transparent;
left: -16px;
top: 0px;
}
.chat-input ::placeholder {
/* Chrome, Firefox, Opera, Safari 10.1+ */
color: white;
opacity: 1;
font-size: 1rem;
font-weight: bold;
/* Firefox */
}
.chat-input :-ms-input-placeholder {
/* Internet Explorer 10-11 */
color: white;
font-size: 1rem;
font-weight: bold;
}
.chat-input ::-ms-input-placeholder {
/* Microsoft Edge */
color: white;
font-size: 1rem;
font-weight: bold;
}

View file

@ -0,0 +1,69 @@
/*Choose room styles*/
#choose-room {
flex-direction: column;
justify-content: center;
align-items: center;
}
.choose-card {
color: white;
font-size: 3rem;
font-weight: 500;
margin: 2rem;
height: 300px;
width: 300px;
display: flex;
justify-content: center;
align-items: center;
border-radius: 20px;
background-color: #FF8E9C;
}
#join-section {
justify-content: center;
align-items: center;
}
#join-form {
flex-direction: column;
}
#join-form [type="text"] {
border: none;
border-radius: 20px;
margin: 1rem 0;
font-size: 1.3rem;
padding: .5rem 1rem;
color: gray;
}
#back-btn {
font-size: 1rem;
text-decoration: underline;
}
#back-btn:hover {
font-weight: bold;
cursor: pointer;
}
#join-form [type="submit"] {
border: none;
margin: 1rem;
padding: .5rem 1rem;
font-size: 2.5rem;
font-weight: 600;
color: white;
background-color: #00CBCC;
border-radius: 15px;
}
@media (min-width: 700px) {
#choose-room {
flex-direction: row;
justify-content: center;
align-items: center;
}
}

View file

@ -1,4 +1,7 @@
@import url('https://fonts.googleapis.com/css2?family=Montserrat:wght@400;500&display=swap');
@import './choose.css';
@import './chat.css';
html, body {
height: 100vh;
overflow: hidden;
@ -8,41 +11,16 @@ html, body {
background-color: #00E5E5;
}
h1, h2, h3 {
margin: 0;
}
section {
height: 100%;
width: 100%;
display: flex;
}
#choose-room {
flex-direction: column;
justify-content: center;
align-items: center;
}
.choose-card {
color: white;
font-size: 3rem;
font-weight: 500;
margin: 2rem;
height: 300px;
width: 300px;
display: flex;
justify-content: center;
align-items: center;
border-radius: 20px;
background-color: #FF8E9C;
}
#join-section {
justify-content: center;
align-items: center;
}
#join-form {
flex-direction: column;
}
.transition-scale {
transition: all 200ms;
}
@ -52,45 +30,6 @@ section {
transform: scale(1.07);
}
#join-form [type="text"] {
border: none;
border-radius: 20px;
margin: 1rem 0;
font-size: 1.3rem;
padding: .5rem 1rem;
color: gray;
}
#back-btn {
font-size: 1rem;
text-decoration: underline;
}
#back-btn:hover {
font-weight: bold;
cursor: pointer;
}
input:focus {
outline: none;
}
#join-form [type="submit"] {
border: none;
margin: 1rem;
padding: .5rem 1rem;
font-size: 2.5rem;
font-weight: 600;
color: white;
background-color: #00CBCC;
border-radius: 15px;
}
@media (min-width: 700px) {
#choose-room {
flex-direction: row;
justify-content: center;
align-items: center;
}
}

View file

@ -17,6 +17,7 @@ path = "src/bin.rs"
rocket = "0.4"
serde = { version = "1.0", features = ["derive"] }
uuid = { version = "0.8", features = ["serde", "v4"] }
rocket_cors = "0.5.0-beta-2"
[dependencies.rocket_contrib]
version = "0.4"

View file

@ -7,7 +7,7 @@ extern crate uuid;
use lib::rooms::rooms_map::RoomsMap;
use rocket::{
http::{RawStr, Status},
http::Status,
response::status::Custom,
State,
};
@ -23,6 +23,8 @@ use std::{
time::Duration,
};
use lib::cors::options;
#[derive(Serialize)]
struct RoomInfo {
addr: SocketAddr,
@ -30,11 +32,11 @@ struct RoomInfo {
}
#[get("/rooms/<id>")]
fn get_addr(id: &RawStr, rooms: State<Arc<Mutex<RoomsMap>>>) -> Result<String, Custom<String>> {
fn get_addr(id: String, rooms: State<Arc<Mutex<RoomsMap>>>) -> Result<Json<RoomInfo>, Custom<String>> {
let arc_clone = _get_state_arc(&rooms);
let rooms = arc_clone.lock().unwrap();
match rooms.get_addr(id) {
Ok(addr) => Ok(addr.to_string()),
match rooms.get_addr(&id) {
Ok(addr) => Ok(Json(RoomInfo{id, addr})),
Err(_) => Err(Custom(
Status::ImATeapot,
"Can't get the address".to_string(),
@ -50,6 +52,7 @@ fn create_room(state: State<Arc<Mutex<RoomsMap>>>) -> Result<Json<RoomInfo>, Cus
match rooms.start_room(id.clone()) {
Ok(addr) => {
_close_timeout(id.clone(), &state);
println!("Room with {} created!", &id);
Ok(Json(RoomInfo { addr, id }))
}
Err(message) => Err(Custom(Status::Locked, message)),
@ -85,5 +88,6 @@ fn main() {
rocket::ignite()
.mount("/", routes![get_addr, close_room, create_room])
.manage(Arc::new(Mutex::new(RoomsMap::new())))
.attach(options())
.launch();
}

29
server/src/cors.rs Normal file
View file

@ -0,0 +1,29 @@
use rocket::http::Method;
/**
* Copyright [2020] [Dario Alessandro Lencina Talarico]
* Licensed under the Apache License, Version 2.0 (the "License");
* y ou may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use rocket_cors::{AllowedHeaders, AllowedOrigins};
pub fn options() -> rocket_cors::Cors {
let allowed_origins = AllowedOrigins::All;
rocket_cors::CorsOptions {
allowed_origins,
allowed_methods: vec![Method::Get, Method::Post, Method::Delete]
.into_iter()
.map(From::from)
.collect(),
allowed_headers: AllowedHeaders::All,
..Default::default()
}
.to_cors()
.expect("Not to fail")
}

View file

@ -1 +1,2 @@
pub mod rooms;
pub mod cors;

View file

@ -43,6 +43,7 @@ impl RoomsMap {
Some(controller) => {
match controller.shutdown_room() {
Ok(_) => {
println!("Room {} closed!!", &id);
self.0.remove_entry(&id);
Ok(())
},