-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_client.html
169 lines (145 loc) · 4.93 KB
/
test_client.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>KoFi WebSocket Tester</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 20px auto;
padding: 0 20px;
}
.card {
border: 1px solid #ddd;
padding: 15px;
margin: 10px 0;
border-radius: 5px;
}
.log {
background: #f5f5f5;
padding: 10px;
height: 200px;
overflow-y: auto;
margin: 10px 0;
}
.success {
color: green;
}
.error {
color: red;
}
</style>
</head>
<body>
<h1>KoFi WebSocket Tester</h1>
<div class="card">
<h3>Server Configuration</h3>
<div style="margin-bottom: 10px;">
<label>Base URL:</label><br>
<input type="text" id="baseUrl" placeholder="Base URL" value="http://localhost:8000" style="width: 300px;">
</div>
</div>
<div class="card">
<h3>WebSocket Connection</h3>
<input type="text" id="token" placeholder="Verification Token" value="test_token">
<button onclick="connectWebSocket()">Connect</button>
<button onclick="disconnectWebSocket()">Disconnect</button>
<button onclick="sendPing()">Send Ping</button>
</div>
<div class="card">
<h3>Simulate Webhook</h3>
<button onclick="simulateWebhook()">Send Test Webhook</button>
</div>
<div class="card">
<h3>Log</h3>
<div id="log" class="log"></div>
</div>
<script>
let ws = null;
function log(message, isError = false) {
const logDiv = document.getElementById('log');
const entry = document.createElement('div');
entry.classList.add(isError ? 'error' : 'success');
entry.textContent = `${new Date().toLocaleTimeString()}: ${message}`;
logDiv.appendChild(entry);
logDiv.scrollTop = logDiv.scrollHeight;
}
function getBaseUrl() {
return document.getElementById('baseUrl').value.replace(/\/$/, '');
}
function connectWebSocket() {
const token = document.getElementById('token').value;
const baseUrl = getBaseUrl().replace('http', 'ws');
// Combine the endpoint and token correctly
ws = new WebSocket(`${baseUrl}/ws/${token}`);
ws.onopen = () => {
log('WebSocket connected');
};
ws.onmessage = (event) => {
log(`Received: ${event.data}`);
if (typeof event.data === 'string' && event.data !== 'pong') {
const data = JSON.parse(event.data);
}
};
ws.onclose = () => {
log('WebSocket disconnected', true);
ws = null;
};
ws.onerror = (error) => {
log(`WebSocket error: ${error.message}`, true);
};
}
function disconnectWebSocket() {
if (ws) {
ws.close();
ws = null;
}
}
function sendPing() {
if (ws) {
ws.send('ping');
log('Sent: ping');
} else {
log('WebSocket not connected', true);
}
}
function simulateWebhook() {
const token = document.getElementById('token').value;
const baseUrl = getBaseUrl();
const webhookData = {
verification_token: token,
message_id: `test_${Date.now()}`,
timestamp: new Date().toISOString(),
type: "Donation",
is_public: true,
from_name: "Test User",
message: "Test webhook message",
amount: "5.00",
url: "https://ko-fi.com/test",
email: "test@example.com",
currency: "USD",
is_subscription_payment: false,
is_first_subscription_payment: false,
kofi_transaction_id: `TX_${Date.now()}`,
shop_items: null,
tier_name: null,
shipping: null
};
// Create form data with proper encoding
const formData = new URLSearchParams();
formData.append('data', JSON.stringify(webhookData));
fetch(`${baseUrl}/webhook`, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: formData
})
.then(response => response.json())
.then(data => log(`Webhook response: ${JSON.stringify(data)}`))
.catch(error => log(`Webhook error: ${error.message}`, true));
}
</script>
</body>
</html>