1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
const $ = require('jquery');
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));
});
/**
* 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;
// setup listener
connectedUser.on('open', () => {
// TODO: Activate chat or sth
});
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><hr>`);
} else {
$('#user_id').val(connectedUserId); // TODO: WTH DOESNT THIS WORK LOL
$('#messages').append(`${message}<hr>`);
}
}
/**
* Events after load
*/
$(document).ready(() => {
$('#user_id_form').on('click', () => connect($('#user_id').val()));
$('#message_form').on('click', () => sendMessage($('#message').val()));
});
|