This repository has been archived by the owner on Feb 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
79 lines (70 loc) · 2.51 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/**
* @author NriotHrreion
* @license MIT
*
* Copyright (c) NriotHrreion 2020
*/
"use strict";
const process = require("process");
const express = require("express");
const fs = require("fs");
// const path = require("path");
const keypress = require("keypress");
const https = require("http");
const ws = require("ws");
const chalk = require("chalk");
var app = express();
var server = https.createServer(app);
var wss = new ws.Server({server});
wss.on("connection", function(ws) {
ws.on("message", function(data) {
data = JSON.parse(data);
switch(data.type) {
case "message":
wss.clients.forEach((client) => {
var user = data.user;
var message = data.message;
client.send(JSON.stringify({type: "message", message: `[${new Date().getHours()}:${new Date().getMinutes()}:${new Date().getSeconds()}] ${user}: ${message}`}));
console.log(chalk.green("[") + new Date() + chalk.green("]") +" "+ chalk.yellow(user) +": "+ message);
});
break;
case "image":
wss.clients.forEach((client) => {
var user = data.user;
var src = data.src;
client.send(JSON.stringify({type: "image", message: `[${new Date().getHours()}:${new Date().getMinutes()}:${new Date().getSeconds()}] ${user}: <img src="${src}"></img>`}));
console.log(chalk.green("[") + new Date() + chalk.green("]") +" "+ chalk.yellow(user) +": [Image]");
});
break;
case "withdraw":
wss.clients.forEach((client) => {
client.send(JSON.stringify({type: "withdraw", id: data.id}));
});
break;
}
});
ws.on("close", function() {
console.log("Client disconnected");
wss.clients.forEach((client) => {
client.send("Client disconnected");
});
});
});
server.listen(3500, function() {
console.log("Server Start!");
console.log("You can press ESC to close the server");
});
var chat_app = express();
chat_app.get("/", (req, res) => {
res.set("Content-Type","text/html;charset=utf-8");
res.send(fs.readFileSync("./client/index.html").toString());
});
chat_app.listen(8920);
keypress(process.stdin);
process.stdin.on("keypress", (ch, key) => {
if(key && key.name == "escape") {
process.exit();
}
});
process.stdin.setRawMode(true);
process.stdin.resume();