Gerçek Zamanlı İletişim İstemcisi
Vercel üzerindeki sunucunuzla iletişim kurmak için örnek bir uygulama.
Uygulama Hakkında
Bu basit web sayfası, JavaScript'in fetch API'ını kullanarak bir sunucuya HTTPS isteği göndermek için tasarlanmıştır. Gerçek zamanlı bir sistem kurmak için bu temel mantığı kullanabilir, sunucudan gelen cevapları anında ekrana yansıtabilirsiniz. İsteğin gövdesine (body) JSON formatında veri ekleyebilir ve sunucudan gelen cevabı işleyebilirsiniz.
Mesaj Gönder
HTTPS İsteği Nasıl Atılır?
Vercel'deki sunucunuza bir POST isteği göndermek için kullanabileceğiniz örnek JavaScript kodu.
async function sendMessage(message) {
const serverUrl = 'https://your-vercel-project.vercel.app/api/your-endpoint'; // Vercel API endpoint
try {
const response = await fetch(serverUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ text: message }),
});
if (!response.ok) {
throw new Error(`HTTP Error! Status: ${response.status}`);
}
const data = await response.json();
console.log('Response from server:', data);
// Call function to display server response
// displayMessage(data.response, 'received');
} catch (error) {
console.error('An error occurred during the request:', error);
// displayMessage('Error: ' + error.message, 'received');
}
}