Skip to content

Commit 18336a2

Browse files
committed
web secure socket & fix player already in server kicking
1 parent d2e45e2 commit 18336a2

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

src/client/scripts/Setup.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,15 @@ async function Setup(game) {
4343
game.distanceBasedFog = new DistanceBasedFog(game);
4444
UrlParams(game, (password) => {
4545
console.warn(gpuInfo());
46-
46+
var prefix;
47+
if (document.location.protocol === "https:") {
48+
prefix = "wss";
49+
} else {
50+
prefix = "ws";
51+
}
4752
game.socket = new Socket(
4853
game,
49-
`ws://${document.location.host}?nick=${game.nick}&server=${game.server}&port=${game.serverPort}&password=${password}&premium=${game.premium}`
54+
`${prefix}://${document.location.host}?nick=${game.nick}&server=${game.server}&port=${game.serverPort}&password=${password}&premium=${game.premium}`
5055
);
5156

5257
game.pii = new PlayerInInventory(game);

src/client/scripts/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ class Game {
3939
async init() {
4040
await this.al.init();
4141
await Setup(this);
42+
this.socket.on("alreadyPlaying", () => {
43+
swal({
44+
title: "Player already is in server",
45+
text: "Try later...",
46+
icon: "error",
47+
button: "Rejoin",
48+
}).then(function () {
49+
document.location.reload();
50+
});
51+
});
4252
this.socket.on("connect", () => {
4353
console.log("Connected to server!");
4454
$(".loadingText").text(`Connecting to ${this.server}`);

src/index.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,18 @@ server.listen(port, function () {
4444
return console.log(`Server is running on \x1b[34m*:${port}\x1b[0m`);
4545
});
4646

47-
var botByNick = {};
47+
var botByNick = new Map();
4848

4949
wss.on("connection", (socket, req) => {
5050
const emit = (type, ...data) => {
5151
socket.send(encode([type, ...data]));
5252
};
53-
5453
const query = qs.parse(req.url.substr(1), { ignoreQueryPrefix: true });
5554

55+
if (botByNick.get(query.nick) !== undefined) {
56+
emit("alreadyPlaying");
57+
return;
58+
}
5659
console.log(`[\x1b[32m+\x1b[0m] ${query.nick}`);
5760
var heldItem = null;
5861
var bot = mineflayer.createBot({
@@ -62,7 +65,7 @@ wss.on("connection", (socket, req) => {
6265
version: version,
6366
password: query.premium === "true" ? query.password : undefined,
6467
});
65-
botByNick[query.nick] = bot;
68+
botByNick.set(query.nick, bot);
6669
bot._client.on("map_chunk", function (packet) {
6770
var cell = new Chunk();
6871
cell.load(packet.chunkData, packet.bitMap, true, true);
@@ -220,6 +223,7 @@ wss.on("connection", (socket, req) => {
220223
try {
221224
clearInterval(interval);
222225
console.log(`[\x1b[31m-\x1b[0m] ${query.nick}`);
226+
botByNick.delete(query.nick);
223227
bot.end();
224228
} catch (error) {}
225229
});

0 commit comments

Comments
 (0)