diff options
author | Marvin Borner | 2019-01-23 22:31:53 +0100 |
---|---|---|
committer | Marvin Borner | 2019-01-23 22:31:53 +0100 |
commit | 4603dd25d9d448e3c8d9d1280cb59c9a66906cf7 (patch) | |
tree | 0346966052e5b67d219ab6eb5f3daf3014bec628 /public/scripts/chat.js | |
parent | 9c462ef4be0a7510607ac1d06b57cbe97e71d26a (diff) |
Added prettier exporting and encryption check
Diffstat (limited to 'public/scripts/chat.js')
-rw-r--r-- | public/scripts/chat.js | 121 |
1 files changed, 67 insertions, 54 deletions
diff --git a/public/scripts/chat.js b/public/scripts/chat.js index b33d503..e8b53c0 100644 --- a/public/scripts/chat.js +++ b/public/scripts/chat.js @@ -1,70 +1,83 @@ const $ = require('jquery'); +const encryption = require('./2_encryption'); const nanoid = require('nanoid'); let connectedUserId, connectedUser; const userId = nanoid(); -const peer = new Peer(userId, {host: '127.0.0.1', port: 4242, path: '/', debug: 3}); -// Peer events -peer.on('open', id => console.log('[LOG] Your ID is', id)); -peer.on('error', err => console.error(err)); -peer.on('connection', conn => { - connectedUser = conn; - console.log('[LOG] Connected with', conn.peer); - conn.on('data', message => receivedMessage(message)); -}); +// setup encryption +if (encryption.check()) { + // TODO: Ask for password + chat(); +} else { + console.log('[LOG] No existing keys found! Generating...'); + encryption.generate(userId, 'supersecure').then(() => chat()); +} -/** - * Connects to an user via his id - * @param id - */ -function connect(id) { - const connectionId = nanoid(); - console.log('[LOG] Connecting to', id); - console.log('[LOG] Your connection ID is', connectionId); - connectedUser = peer.connect(id, {label: connectionId, reliable: true}); - connectedUserId = id; +function chat() { + const peer = new Peer(userId, {host: '127.0.0.1', port: 4242, path: '/', debug: 3}); - // setup listener - connectedUser.on('open', () => { - // TODO: Activate chat or sth - // TODO: Send public key + // Peer events + peer.on('open', id => console.log('[LOG] Your ID is', id)); + peer.on('error', err => console.error(err)); + peer.on('connection', conn => { + connectedUser = conn; + console.log('[LOG] Connected with', conn.peer); + conn.on('data', message => receivedMessage(message)); }); - connectedUser.on('data', message => receivedMessage(message)) -} + /** + * Connects to an user via his id + * @param id + */ + function connect(id) { + const connectionId = nanoid(); + console.log('[LOG] Connecting to', id); + console.log('[LOG] Your connection ID is', connectionId); + connectedUser = peer.connect(id, {label: connectionId, reliable: true}); + connectedUserId = id; -/** - * Sends a message to the user with which you're currently connected - * @param message - */ -function sendMessage(message) { - console.log(`[LOG] Sending message ${message} to ${connectedUserId}`); - connectedUser.send(message); - receivedMessage(message, true); -} + // setup listener + connectedUser.on('open', () => { + // TODO: Activate chat or sth + // TODO: Send public key + }); -/** - * Renders the incoming messages - * @param message - * @param self - */ -function receivedMessage(message, self = false) { - if (self) { - $('#messages').append(`<span style="color: green">${message}</span><br>`); - } else { - $('#messages').append(`${message}<br>`); + connectedUser.on('data', message => receivedMessage(message)) + } + + /** + * Sends a message to the user with which you're currently connected + * @param message + */ + function sendMessage(message) { + console.log(`[LOG] Sending message ${message} to ${connectedUserId}`); + connectedUser.send(message); + receivedMessage(message, true); + } + + /** + * Renders the incoming messages + * @param message + * @param self + */ + function receivedMessage(message, self = false) { + if (self) { + $('#messages').append(`<span style="color: green">${message}</span><br>`); + } else { + $('#messages').append(`${message}<br>`); + } } -} -/** - * Events after load - */ -$(document).ready(() => { - $('#add_user_id').on('click', () => connect($('#user_id').val())); - $('#send_message').on('click', () => sendMessage($('#message').val())); + /** + * Events after load + */ + $(document).ready(() => { + $('#add_user_id').on('click', () => connect($('#user_id').val())); + $('#send_message').on('click', () => sendMessage($('#message').val())); - $('[toggle-contact-modal]').on('click', () => $('#add_contact_modal').toggleClass('is-active')) -}); + $('[toggle-contact-modal]').on('click', () => $('#add_contact_modal').toggleClass('is-active')) + }); +} -testEncryption(); +//encryption.test(); // TESTING IF ENCRYPTION WORKS |