https -> http

This commit is contained in:
leca 2023-08-03 14:40:46 +03:00
parent 6431b2c4e0
commit 4891f0c083
3 changed files with 8 additions and 31 deletions

View File

@ -1,6 +1,6 @@
var serverAddress = document.getElementById("server-address").value; var serverAddress = document.getElementById("server-address").value;
var serverPort = document.getElementById("server-port").value var serverPort = document.getElementById("server-port").value
var socket//new WebSocket(`wss://${serverAddress}:${serverPort}`); var socket
var canvas = document.getElementById("board"); var canvas = document.getElementById("board");
var colorInput = document.getElementById("color"); var colorInput = document.getElementById("color");
@ -30,15 +30,13 @@ function connect() {
serverAddress = document.getElementById("server-address").value; serverAddress = document.getElementById("server-address").value;
serverPort = document.getElementById("server-port").value serverPort = document.getElementById("server-port").value
console.log(`Connecting ${serverAddress}:${serverPort}`) console.log(`Connecting ${serverAddress}:${serverPort}`)
socket = new WebSocket(`wss://${serverAddress}:${serverPort}`) socket = new WebSocket(`ws://${serverAddress}:${serverPort}`)
// socket = new WebSocket(`wss://127.0.0.1:8080`)
timer.textContent="Board is loading, please wait"; timer.textContent="Board is loading, please wait";
socket.addEventListener("open", (event) => { socket.addEventListener("open", (event) => {
socket.send("{\"code\":0}"); socket.send(JSON.stringify({code:0}));
}); });
socket.addEventListener("message", (event) => { socket.addEventListener("message", (event) => {
// console.log("Message from server ", JSON.stringify(event.data.toString()));
event.data.text().then(function(packet){ event.data.text().then(function(packet){
packet = JSON.parse(packet) packet = JSON.parse(packet)
let code = packet.code let code = packet.code

View File

@ -2,9 +2,7 @@
"httpPort": 8080, "httpPort": 8080,
"boardWidth": 1920, "boardWidth": 1920,
"boardHeight": 1080, "boardHeight": 1080,
"serverAddress": "127.0.0.1", "serverAddress": "localhost",
"saveFile": "./board.png", "saveFile": "./board.png",
"timeBetweenPixels": 5, "timeBetweenPixels": 5
"httpsCertFile": "",
"httpsKeyFile": ""
} }

View File

@ -3,7 +3,6 @@ const path = require('path');
const pug = require('pug'); const pug = require('pug');
const Jimp = require('jimp'); const Jimp = require('jimp');
const fs = require('fs'); const fs = require('fs');
const https = require('https');
const http = require('http'); const http = require('http');
const express = require('express'); const express = require('express');
const app = express(); const app = express();
@ -18,10 +17,6 @@ const saveFile = config.saveFile;
const boardWidth = config.boardWidth; const boardWidth = config.boardWidth;
const boardHeight = config.boardHeight; const boardHeight = config.boardHeight;
const timeBetweenPixels = config.timeBetweenPixels; const timeBetweenPixels = config.timeBetweenPixels;
const credentails = {
key: fs.readFileSync(config.httpsKeyFile),
cert: fs.readFileSync(config.httpsCertFile)
}
var toQuit = false; var toQuit = false;
@ -48,27 +43,14 @@ if (!fs.existsSync(saveFile)) {
} }
const httpsServer = https const httpServer = http
.createServer( .createServer(
credentails,
app app
) )
.listen(httpPort, "0.0.0.0", () => { .listen(httpPort, "0.0.0.0", () => {
console.log("test))0");
// const ws = new WebSocket(`wss://127.0.0.1:${httpPort}`, {
// rejectUnauthorized: false
// });
// ws.on('error', console.error);
// ws.on('message', (msg) => {
// console.log("Client got a message: ", msg.toString())
// })
// ws.on('open', function open() {
// ws.send('All glory to WebSockets!');
// });
}) })
const server = new WebSocket.Server({ const server = new WebSocket.Server({
server: httpsServer, server: httpServer,
host: "0.0.0.0", host: "0.0.0.0",
}); });
@ -109,7 +91,7 @@ server.on('connection', (client) => {
response.pixelOwner = false; response.pixelOwner = false;
contentJson = JSON.parse(content); contentJson = JSON.parse(content);
let pixelNumber = evaulatePixelNumber(contentJson.x * 4, contentJson.y * 4); let pixelNumber = evaulatePixelNumber(contentJson.x * 4, contentJson.y * 4);
if (contentJson.x < 0 || contentJson.y < 0) client.send("{\"code\":-1}"); if (contentJson.x < 0 || contentJson.y < 0) client.send(JSON.stringify("{code:-1}"));
let unixTime = Math.floor(new Date().getTime() / 1000); let unixTime = Math.floor(new Date().getTime() / 1000);
if (lastPixelTimestamp[client.id] == undefined || unixTime - lastPixelTimestamp[client.id] > timeBetweenPixels - 1) { if (lastPixelTimestamp[client.id] == undefined || unixTime - lastPixelTimestamp[client.id] > timeBetweenPixels - 1) {
@ -144,7 +126,6 @@ server.on('connection', (client) => {
app.get('/', (req, res) => { app.get('/', (req, res) => {
// res.send("Hello")
res.render('index.pug', { root: __dirname, server: serverAddress, port: httpPort }); res.render('index.pug', { root: __dirname, server: serverAddress, port: httpPort });
}) })
app.use(function (err, req, res, next) { app.use(function (err, req, res, next) {