diff options
author | Marvin Borner | 2019-01-22 21:38:55 +0100 |
---|---|---|
committer | Marvin Borner | 2019-01-22 21:38:55 +0100 |
commit | 3de7505e2b0a2daa6386ab161e9b8520870c1393 (patch) | |
tree | 6b7531f6ca2893654b271233e890987ed2bfa99f /public/scripts | |
parent | 92648c3e1b8b15d5edfd3f8d6e9c28b486ba6364 (diff) |
Some more functionality
Diffstat (limited to 'public/scripts')
-rw-r--r-- | public/scripts/main.js | 33 |
1 files changed, 22 insertions, 11 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())); }); |