diff options
-rw-r--r-- | public/scripts/main.js | 33 | ||||
-rw-r--r-- | views/index.pug | 4 |
2 files changed, 24 insertions, 13 deletions
diff --git a/public/scripts/main.js b/public/scripts/main.js index d0d6073..ee63357 100644 --- a/public/scripts/main.js +++ b/public/scripts/main.js @@ -1,30 +1,41 @@ const $ = require('jquery'); +const util = require('util'); 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('data', data => console.log('[LOG] Received data ' + data)); - peer.on('error', err => console.error(err)); -}); +peer.on('open', id => console.log('[LOG] Your ID is', id)); +peer.on('connection', conn => console.log('[LOG] Connected with', conn.peer)); +peer.on('error', err => console.error(err)); function connect(id) { - console.log('[LOG] Connecting to ' + id); const connectionId = nanoid(); - const conn = peer.connect(id, {label: connectionId}); - console.log('[LOG] Your connection ID is ' + connectionId); + console.log('[LOG] Connecting to', id); + console.log('[LOG] Your connection ID is', connectionId); + connectedUser = peer.connect(id, {label: connectionId, reliable: true}); + connectedUserId = id; - conn.on('open', function () { - conn.send('hi!'); + // setup listener + connectedUser.on('open', () => { + connectedUser.send('Hi!'); + // TODO: Activate chat or sth }); + + connectedUser.on('data', data => console.log('[LOG] Received data', data)); +} + +function sendMessage(message) { + console.log(`[LOG] Sending message ${message} to ${connectedUserId}`); + connectedUser.send(message); } /** * Events after load */ $(document).ready(() => { - $('#user_id_form').on('click', e => connect($('#user_id').val())); + $('#user_id_form').on('click', () => connect($('#user_id').val())); + $('#message_form').on('click', () => sendMessage($('#message').val())); }); diff --git a/views/index.pug b/views/index.pug index 75ce696..9afc2b3 100644 --- a/views/index.pug +++ b/views/index.pug @@ -11,8 +11,8 @@ block content pre#incoming.nes-container.with-title .form.nes-container.with-title .field - label(for='outgoing') Message - textarea#outgoing.nes-textarea + label(for='message') Message + textarea#message.nes-textarea button#message_form.nes-btn.is-primary(type=submit) Submit .nes-container.with-title .field |