WebSocket upgrade

웹소켓 업그레이드

An HTTP handshake opens a connection that becomes a bidirectional message channel.

···
html
<div class="n-demo"><div class="n-head"><strong>HTTP → WEBSOCKET</strong><span>LIVE TRACE</span></div><div class="n-stage"><div class="ws n-box">CLIENT</div><div class="ws-center"><div id="ws-kind">Upgrade request →</div><div id="ws-line"></div><div id="ws-message">waiting for 101</div></div><div class="ws n-box">SERVER</div></div><div class="n-foot">tap to step · auto replay</div></div>
css
.n-demo{width:min(94vw,760px);height:min(88vh,324px);padding:clamp(9px,2.2vmin,18px);border:1px solid var(--line);border-radius:13px;background:var(--surface);display:flex;flex-direction:column;gap:clamp(5px,1.3vmin,10px);font:600 clamp(12px,2.4vmin,16px)/1.3 "Pretendard Variable",Pretendard,-apple-system,BlinkMacSystemFont,"Apple SD Gothic Neo",sans-serif;overflow:hidden}.n-head,.n-foot{display:flex;justify-content:space-between;align-items:center;color:var(--muted)}.n-head strong{color:var(--accent)}.n-head span,.n-foot{font-size:12px}.n-stage{flex:1;min-height:0;position:relative;display:flex;align-items:center;justify-content:center;gap:clamp(4px,1vmin,10px)}.mono{font-family:ui-monospace,SFMono-Regular,Consolas,monospace}.n-box{padding:clamp(5px,1.5vmin,11px);border:1px solid var(--line);border-radius:8px;background:var(--bg);text-align:center}.n-on{border-color:var(--accent)!important;color:var(--accent)!important;background:color-mix(in srgb,var(--accent) 12%,var(--surface))!important}.n-muted{color:var(--muted)}@media(max-width:400px){.n-head span{display:none}}.ws{width:21%}.ws-center{width:52%;text-align:center;display:grid;gap:8px}.ws-center div:first-child{color:var(--accent)}#ws-line{height:3px;background:var(--line);border-radius:3px;position:relative}#ws-line:after{content:"";position:absolute;top:-5px;left:0;width:13px;height:13px;border-radius:50%;background:var(--accent);animation:travel 1.05s linear infinite alternate}@keyframes travel{to{left:calc(100% - 13px)}}#ws-message{color:var(--muted)}
js
let step=0;function advance(){const labels=['Upgrade request →','← 101 Switching Protocols','message →','← message'];document.getElementById('ws-kind').textContent=labels[step];document.getElementById('ws-message').textContent=step<2?'HTTP handshake':'two-way channel';step=(step+1)%4}
const clickTarget=document.querySelector('.n-demo');if(clickTarget&&typeof advance==='function'){advance();const timer=setInterval(advance,1050);clickTarget.addEventListener('pointerdown',()=>{clearInterval(timer);advance()})}

WebSocket begins with an HTTP upgrade handshake. Once accepted, client and server exchange messages in both directions on the open connection.

The demo alternates message direction after the upgrade. Real-time applications must still handle authentication, connection lifetime, and reconnection.

When to use

Consider it for chat or collaboration when both sides frequently initiate messages.

Open as page ↗