diff options
author | Marvin Borner | 2019-02-22 18:01:33 +0100 |
---|---|---|
committer | Marvin Borner | 2019-02-22 18:01:33 +0100 |
commit | 8ce84878c086ebf8da240a119838255c56b7059d (patch) | |
tree | 0e518136c07f18442fd4fab92a6fbd66c1253248 /public | |
parent | d3b3274949593b73da79608aeedf1fae7954a152 (diff) |
Fixed connection requests
Diffstat (limited to 'public')
-rw-r--r-- | public/scripts/chat.js | 33 |
1 files changed, 16 insertions, 17 deletions
diff --git a/public/scripts/chat.js b/public/scripts/chat.js index b89900c..494e1c8 100644 --- a/public/scripts/chat.js +++ b/public/scripts/chat.js @@ -130,6 +130,7 @@ function chat() { } }); + // This event gets fired when the initiator wants to connect to the peer peer.on('connection', (conn) => { swal({ title: 'Connection request', @@ -145,8 +146,8 @@ function chat() { type: 'state', data: 'accepted', }); - connectedPeer.on('data', async (state) => { - if (state.data === 'received') { + connectedPeer.on('data', async (data) => { + if (data.type === 'state' && data.data === 'received') { console.log('[LOG] Connected to', connectedPeer.peer); swal( 'New connection!', @@ -157,12 +158,11 @@ function chat() { connectedPeer.peer, await encryption.getPeerPublicKey(connectedPeer.peer), ) - .then(messages => messages.forEach(async data => await receivedMessage(`${data.message} - ${data.time}`, true))); - connectedPeer.on('open', async () => transferKey(await encryption.getPublicKey())); - connectedPeer.on('data', async (message) => { - console.log('[LOG] Received new message!'); - await receivedMessage(message); - }); + .then(messages => messages.forEach(async messageData => receivedMessage(`${messageData.message} - ${messageData.time}`, true))); + transferKey(await encryption.getPublicKey()); + } else if (data.type !== 'state') { + console.log('[LOG] Received new message!'); + await receivedMessage(data); } }); } else { @@ -177,7 +177,7 @@ function chat() { }); /** - * Connects to a peer via his id + * Connects the initiator to a peer via his id * @param id * @returns {Promise<void>} */ @@ -186,8 +186,8 @@ function chat() { console.log('[LOG] Connecting to', id); console.log('[LOG] Your connection ID is', connectionId); connectedPeer = peer.connect(id, { label: connectionId }); - connectedPeer.on('data', async (state) => { - if (state.type === 'state' && state.data === 'accepted') { + connectedPeer.on('data', async (data) => { + if (data.type === 'state' && data.data === 'accepted') { connectedPeer.send({ type: 'state', data: 'received', @@ -199,16 +199,15 @@ function chat() { connectedPeer.peer, await encryption.getPeerPublicKey(connectedPeer.peer), ) - .then(messages => messages.forEach(async data => await receivedMessage(`${data.message} - ${data.time}`, true))); - connectedPeer.on('data', async (message) => { - console.log('[LOG] Received new message!'); - await receivedMessage(message); - }); + .then(messages => messages.forEach(async messageData => receivedMessage(`${messageData.message} - ${messageData.time}`, true))); connectedPeer.on('close', () => { swal('Disconnected!', `The connection to "${connectedPeer.peer}" has been closed!`, 'error'); }); - } else if (state.type === 'state') { + } else if (data.type === 'state') { swal('Declined!', `The user "${connectedPeer.peer}" has declined your connection request.`, 'error'); + } else { + console.log('[LOG] Received new message!'); + await receivedMessage(data); } }); } |