blob: c715fbd6e22d5a18003d3db0b672fb2432bbb033 (
plain) (
blame)
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
80
81
82
|
/************
GENERATE KEYS
************/
if (localStorage.getItem('KeysGenerated') === null || localStorage.getItem('KeysGenerated') !== "true") {
// GENERATE -- LATER ON LOGIN!
var EncryptionPhrase = "PASSWORD 123"; // THE USERS PASSWORD
var RSABitLength = 1024;
var PrivateKeyString = cryptico.generateRSAKey(EncryptionPhrase, RSABitLength);
var PublicKeyString = cryptico.publicKeyString(PrivateKeyString);
// SAVE TO DATABASE
$.ajax({
type: "POST",
url: "assets/php/SavePublicKey.php",
data: {
UserID: "1", // TEMPORARY
PublicKeyString: PublicKeyString
},
async: true,
error: function () {
console.error("Error while saving public key to database!");
},
success: function () {
localStorage.setItem('KeysGenerated', "true");
}
});
}
/******
GENERAL
******/
var ChatTextInput = $("#ChatTextInput");
var SubscribeTextInput = $("#SubscribeTextInput");
var ChatResponses = $("#ChatResponses");
var WebSocket = new WebSocket('wss://marvinborner.ddnss.de:1337');
WebSocket.onopen = function () {
console.log("Chat connection established!");
};
WebSocket.onmessage = function (e) {
var MessageObject = JSON.parse(e.data);
if (MessageObject.ServerMessage === false) {
ChatResponses.append(MessageObject.Username + " - " + MessageObject.Message + "<br>");
} else if (MessageObject.ServerMessage === true) {
if (MessageObject.ServerMessageType === "GroupJoin") {
if (MessageObject.WasHimself === false) {
ChatResponses.append(MessageObject.Username + " <span data-lang='joined the group'></span><br>");
} else if (MessageObject.WasHimself === true) {
ChatResponses.empty();
ChatResponses.append("<span data-lang='You joined the group'> " + MessageObject.GroupName + "</span>.<br>");
}
} else if (MessageObject.ServerMessageType === "UserDisconnect") {
ChatResponses.append(MessageObject.Username + " <span data-lang='has disconnected from the server'></span><br>");
}
}
initiateLanguage(); // need further work (performance)
};
ChatTextInput.keyup(function (e) {
if (e.keyCode === 13) {
sendMessage(ChatTextInput.val());
ChatTextInput.val("");
}
});
SubscribeTextInput.keyup(function (e) {
if (e.keyCode === 13) {
subscribe(SubscribeTextInput.val());
}
});
function subscribe(channel) {
WebSocket.send(JSON.stringify({ClientMessageType: "Subscribe", Channel: channel}));
SubscribeTextInput.hide();
ChatTextInput.show();
}
function sendMessage(msg) {
WebSocket.send(JSON.stringify({ClientMessageType: "Message", Message: msg}));
ChatTextInput.val("");
}
|