Fundamentos do protocolo WebSocket
Aprofunde os conhecimentos sobre o protocolo WebSocket subjacente, incluindo o handshake, as tramas e o ciclo de vida da ligação.
Fundamentos do protocolo WebSocket é uma aula grátis de WebSockets & Real-Time Systems with Spring no CoddyKit. Esta é a aula 3 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de WebSockets & Real-Time Systems with Spring, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de WebSockets & Real-Time Systems with Spring inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em inglês.
Beyond HTTP: The WebSocket Protocol
WebSockets feel like HTTP but are a distinct protocol. They open a persistent, full-duplex link so data flows both ways at once after setup.
Starting with a Handshake
Every WebSocket starts with a handshake: the client sends a normal HTTP request asking the server to upgrade the connection to WebSocket.
Client's Upgrade Request
The client's upgrade request is an HTTP GET with key headers: Upgrade: websocket, Connection: Upgrade, plus Sec-WebSocket-Key and Version.
Server's Acknowledgment
If the server accepts, it replies with 101 Switching Protocols and a Sec-WebSocket-Accept header derived from the client's key, confirming the upgrade.
Witnessing the Handshake
Watch the handshake yourself: run this in your console, open the Network tab, and look for the 101 Switching Protocols response.
/*
Open your browser's developer console (F12),
go to the 'Network' tab, then paste and run this code.
Look for a '101 Switching Protocols' response!
*/
let ws = new WebSocket("wss://echo.websocket.org");
ws.onopen = (event) => {
console.log("WebSocket connection opened!");
};
ws.onclose = (event) => {
console.log("WebSocket connection closed!");
};
ws.onerror = (error) => {
console.error("WebSocket error:", error);
};
// We'll send messages in later lessons!Connection Established: Full-Duplex
After the handshake, the connection upgrades to a raw TCP link that is full-duplex: client and server send and receive independently, anytime.
How Messages are Sent: Frames
WebSocket data travels in small units called frames. Framing lets large messages fragment efficiently and supports different control message types.
Different Types of Frames
WebSocket frame types each have a job: text for strings like JSON, binary for raw data, ping/pong for heartbeats, and close to end the connection.
The Connection Journey: Lifecycle
A WebSocket follows a clear lifecycle: opening (handshake), open (exchanging frames), closing (graceful shutdown), and closed.
Quick Check: WebSocket Fundamentals
Which of the following are true about a WebSocket connection after a successful handshake?
Recap: WebSocket Protocol Essentials
You demystified the WebSocket protocol: an HTTP handshake upgrades to a persistent full-duplex channel, data moves in frames, and connections follow a lifecycle. Next: comparisons.
Perguntas Frequentes
A aula “Fundamentos do protocolo WebSocket” é grátis?
Sim — o texto completo de “Fundamentos do protocolo WebSocket” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de WebSockets & Real-Time Systems with Spring, atualize para CoddyKit PRO. O curso de WebSockets & Real-Time Systems with Spring inclui 4 aulas no total.
O que vou aprender em “Fundamentos do protocolo WebSocket”?
Aprofunde os conhecimentos sobre o protocolo WebSocket subjacente, incluindo o handshake, as tramas e o ciclo de vida da ligação. Você pratica WebSockets & Real-Time Systems with Spring com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar WebSockets & Real-Time Systems with Spring?
Nenhuma experiência prévia é necessária. WebSockets & Real-Time Systems with Spring no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 3 de 4.
Quanto tempo leva a aula “Fundamentos do protocolo WebSocket”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de WebSockets & Real-Time Systems with Spring?
Sim. Cada aula de WebSockets & Real-Time Systems with Spring inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- Compreender a comunicação em tempo real
- WebSockets vs. sondagem HTTP
- Fundamentos do protocolo WebSocket
- Eventos enviados pelo servidor versus WebSockets