-
Notifications
You must be signed in to change notification settings - Fork 0
/
index-multiple-device.html
executable file
·113 lines (101 loc) · 3.02 KB
/
index-multiple-device.html
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<!DOCTYPE html>
<html>
<head>
<title>Whatsapp API by Ngekoding</title>
<style>
.client {
border: 1px solid #ccc;
padding: 20px;
box-sizing: border-box;
display: inline-block;
margin: 10px;
}
.hide {
display: none;
}
</style>
</head>
<body>
<div id="app">
<h1>Whatsapp API</h1>
<p>Powered by Ngekoding</p>
<div class="form-container">
<label for="client-id">ID</label><br>
<input type="text" id="client-id" placeholder="Masukkan ID">
<br><br>
<label for="client-description">Deskripsi</label><br>
<textarea rows="3" id="client-description" placeholder="Masukkan deskripsi"></textarea>
<br><br>
<button class="add-client-btn">Tambah Client</button>
</div>
<hr>
<div class="client-container">
<div class="client hide">
<h3 class="title"></h3>
<p class="description"></p>
<img src="" alt="QR Code" id="qrcode">
<h3>Logs:</h3>
<ul class="logs"></ul>
</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.3.0/socket.io.js"></script>
<script>
$(document).ready(function() {
var socket = io();
// Ketika button tambah diklik
$('.add-client-btn').click(function() {
var clientId = $('#client-id').val();
var clientDescription = $('#client-description').val();
var template = $('.client').first().clone()
.removeClass('hide')
.addClass(clientId);
template.find('.title').html(clientId);
template.find('.description').html(clientDescription);
$('.client-container').append(template);
socket.emit('create-session', {
id: clientId,
description: clientDescription
});
});
socket.on('init', function(data) {
$('.client-container .client').not(':first').remove();
console.log(data);
for (var i = 0; i < data.length; i++) {
var session = data[i];
var clientId = session.id;
var clientDescription = session.description;
var template = $('.client').first().clone()
.removeClass('hide')
.addClass(clientId);
template.find('.title').html(clientId);
template.find('.description').html(clientDescription);
$('.client-container').append(template);
if (session.ready) {
$(`.client.${session.id} .logs`).append($('<li>').text('Whatsapp is ready!'));
} else {
$(`.client.${session.id} .logs`).append($('<li>').text('Connecting...'));
}
}
});
socket.on('remove-session', function(id) {
$(`.client.${id}`).remove();
});
socket.on('message', function(data) {
$(`.client.${data.id} .logs`).append($('<li>').text(data.text));
});
socket.on('qr', function(data) {
$(`.client.${data.id} #qrcode`).attr('src', data.src);
$(`.client.${data.id} #qrcode`).show();
});
socket.on('ready', function(data) {
$(`.client.${data.id} #qrcode`).hide();
});
socket.on('authenticated', function(data) {
$(`.client.${data.id} #qrcode`).hide();
});
});
</script>
</body>
</html>