From 3de7505e2b0a2daa6386ab161e9b8520870c1393 Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Tue, 22 Jan 2019 21:38:55 +0100 Subject: Some more functionality --- public/scripts/main.js | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) (limited to 'public/scripts') 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())); }); -- cgit v1.2.3