İstemci Tarafı Olay İşleme
Tarayıcıda `open`, `message`, `error` ve `close` gibi bağlantı olaylarını etkili biçimde yönetin.
İstemci Tarafı Olay İşleme, CoddyKit'te ücretsiz bir WebSockets & Realtime Systems Programming dersidir. Bu, 4 dersinin 3. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, WebSockets & Realtime Systems Programming öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. WebSockets & Realtime Systems Programming kursu toplamda 4 dersten oluşur.
Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.
Intro: Why Events Matter
WebSockets are dynamic! Unlike simple HTTP requests, a WebSocket connection stays open. This means you need a way to know when things happen on that connection.
That's where event handling comes in. It allows your browser application to react to the connection opening, messages arriving, errors, or the connection closing.
Connection Established: `onopen`
The first important event is open. This fires as soon as your WebSocket connection has been successfully established and is ready to send and receive data.
It's your green light to start communicating!
Handling Connection Open
Let's see how to handle the open event. When the connection is ready, a message is logged, and then we send a 'Hello' message.
const ws = new WebSocket('wss://echo.websocket.events');
ws.onopen = (event) => {
console.log('Connection established!');
ws.send('Hello WebSocket Server!');
};
// Other event handlers will be added laterReceiving Data: `onmessage`
Once connected, your server will send data. The message event is triggered every time your client receives data from the server.
The received data is in event.data. This can be text or binary data, which you might need to parse (e.g., JSON).
Processing Incoming Messages
Now, let's add an event listener for incoming messages. We'll log the data we receive back from the server.
const ws = new WebSocket('wss://echo.websocket.events');
ws.onopen = (event) => {
console.log('Connection established!');
ws.send('Hello WebSocket Server!');
};
ws.onmessage = (event) => {
console.log(`Server says: ${event.data}`);
};
// Other event handlers will be added laterCatching Errors: `onerror`
Things can go wrong! Network issues, server problems, or invalid protocols can trigger the error event. This event signals a problem but doesn't always provide specific error details directly.
Always include an error handler for robust applications.
Handling Connection Errors
Here's how to set up an onerror handler. This will catch general errors during the WebSocket connection's lifetime.
const ws = new WebSocket('wss://echo.websocket.events');
ws.onopen = (event) => {
console.log('Connection established!');
ws.send('Hello WebSocket Server!');
};
ws.onmessage = (event) => {
console.log(`Server says: ${event.data}`);
};
ws.onerror = (error) => {
console.error('WebSocket encountered an error!');
// More specific error details might be in the browser console
};
// Other event handlers will be added laterConnection Closed: `onclose`
When a WebSocket connection is deliberately closed by either the client or the server, the close event fires. It provides a code and a reason in the event object.
These details help you understand why the connection ended, which is crucial for debugging or attempting reconnections.
Responding to Connection Close
Let's add the onclose handler. It logs the closure code and reason, which are useful for understanding the connection's end.
const ws = new WebSocket('wss://echo.websocket.events');
ws.onopen = (event) => {
console.log('Connection established!');
ws.send('Hello WebSocket Server!');
};
ws.onmessage = (event) => {
console.log(`Server says: ${event.data}`);
};
ws.onerror = (error) => {
console.error('WebSocket encountered an error!');
};
ws.onclose = (event) => {
console.log(`Connection closed! Code: ${event.code}, Reason: ${event.reason || 'No reason provided'}`);
// Common codes: 1000 (normal), 1001 (going away), 1006 (abnormal closure)
};Event Handling Check
You've learned about the main WebSocket events. Now, let's test your understanding!
Recap: Mastering WebSocket Events
Great job! You've explored the essential client-side WebSocket events:
onopen: Fired when the connection is ready.onmessage: Handles incoming data from the server.onerror: Catches connection-related issues.onclose: Indicates the connection has terminated, providing a code and reason.
Understanding these events is key to building robust and interactive realtime applications!
Sıkça Sorulan Sorular
“İstemci Tarafı Olay İşleme” dersi ücretsiz mi?
Evet — “İstemci Tarafı Olay İşleme” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve WebSockets & Realtime Systems Programming kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. WebSockets & Realtime Systems Programming kursu toplamda 4 dersten oluşur.
“İstemci Tarafı Olay İşleme” dersinde ne öğreneceğim?
Tarayıcıda `open`, `message`, `error` ve `close` gibi bağlantı olaylarını etkili biçimde yönetin. WebSockets & Realtime Systems Programming ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.
WebSockets & Realtime Systems Programming öğrenmeye başlamak için deneyim gerekli mi?
Önceden deneyim gerekmez. CoddyKit'te WebSockets & Realtime Systems Programming, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 3. dersidir.
“İstemci Tarafı Olay İşleme” dersi ne kadar sürer?
Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.
Bu WebSockets & Realtime Systems Programming dersinde kod yazıp çalıştırabilir miyim?
Evet. Her WebSockets & Realtime Systems Programming dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.
Bu kursun tüm dersleri
- Tarayıcı WebSocket API'si Temelleri
- Veri Gönderme ve Alma
- İstemci Tarafı Olay İşleme
- İstemcide Otomatik Yeniden Bağlanma