สัญญาณชีพและการคงการเชื่อมต่อด้วย Ping/Pong
ตรวจจับการเชื่อมต่อ WebSocket ที่ตายแล้วตั้งแต่เนิ่น ๆ ด้วยเฟรม ping/pong และสัญญาณชีพ STOMP เพื่อให้เซิร์ฟเวอร์คืนทรัพยากรและไคลเอ็นต์เชื่อมต่อใหม่ได้อย่างรวดเร็ว
สัญญาณชีพและการคงการเชื่อมต่อด้วย Ping/Pong เป็นบทเรียน WebSockets & Real-Time Systems with Spring ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน WebSockets & Real-Time Systems with Spring และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส WebSockets & Real-Time Systems with Spring มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
The Silent Death Problem
A WebSocket can look open long after the peer has vanished. A laptop lid closes, a network drops, a NAT mapping expires — yet neither side gets a close frame. This is a half-open connection.
Why It Matters
Half-open sockets waste server memory, keep sessions in maps forever, and let the client believe it is still receiving updates. You need an active probe to discover the truth: a heartbeat.
Ping/Pong Control Frames
The WebSocket protocol defines ping and pong control frames. One side sends a ping; a healthy peer must reply with a pong. No pong within a deadline means the connection is dead.
STOMP Heartbeats in Spring
When you use STOMP over WebSocket, Spring exposes heartbeat configuration. The two numbers are the rate (ms) the server sends and the rate it expects to receive heartbeats.
registry.enableSimpleBroker("/topic")
.setHeartbeatValue(new long[]{10000, 10000})
.setTaskScheduler(heartbeatScheduler());A Scheduler Is Required
Heartbeats need a TaskScheduler to fire on time. Without one Spring silently disables them.
@Bean
public ThreadPoolTaskScheduler heartbeatScheduler() {
ThreadPoolTaskScheduler s = new ThreadPoolTaskScheduler();
s.setPoolSize(1);
s.initialize();
return s;
}Client-Side STOMP Heartbeats
The client declares the matching heartbeat interval. Negotiation picks the slower of each side's value.
const client = new StompJs.Client({
brokerURL: 'wss://api.example.com/ws',
heartbeatIncoming: 10000,
heartbeatOutgoing: 10000
});Raw Ping at the Container Level
For plain (non-STOMP) handlers you can send ping frames directly from the server session.
PingMessage ping = new PingMessage(ByteBuffer.wrap(new byte[]{1}));
session.sendMessage(ping);Choosing an Interval
Tune the interval to your needs:
- Too short wastes bandwidth and battery on mobile
- Too long delays detection of dead peers
- 10–30 seconds is a common sweet spot
Beating Idle Timeouts
Proxies and load balancers drop connections that are idle too long (often 60s). A heartbeat doubles as keep-alive traffic that resets those idle timers and stops them from killing a healthy socket.
Reacting to a Missed Beat
When a heartbeat is missed, Spring closes the session and fires afterConnectionClosed. Use that hook to clean up state and let the client trigger its reconnect logic.
@Override
public void afterConnectionClosed(WebSocketSession s, CloseStatus status) {
registry.remove(s.getId());
log.warn("Closed {} - {}", s.getId(), status);
}Heartbeats vs Application Acks
A heartbeat proves the transport is alive; it does not prove your message was processed. For business-level guarantees you still need application acknowledgements on top of heartbeats.
Quick Check
Test your grasp of keep-alives.
Recap
You made connections self-healing:
- Half-open sockets die silently; heartbeats expose them
- Ping/pong control frames probe transport health
- STOMP heartbeats need a
TaskSchedulerand matching client intervals - Heartbeats also defeat proxy idle timeouts
- Use the close callback to clean up and trigger reconnects
คำถามที่พบบ่อย
บทเรียน “สัญญาณชีพและการคงการเชื่อมต่อด้วย Ping/Pong” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “สัญญาณชีพและการคงการเชื่อมต่อด้วย Ping/Pong” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส WebSockets & Real-Time Systems with Spring ให้อัปเกรดเป็น CoddyKit PRO คอร์ส WebSockets & Real-Time Systems with Spring มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “สัญญาณชีพและการคงการเชื่อมต่อด้วย Ping/Pong”
ตรวจจับการเชื่อมต่อ WebSocket ที่ตายแล้วตั้งแต่เนิ่น ๆ ด้วยเฟรม ping/pong และสัญญาณชีพ STOMP เพื่อให้เซิร์ฟเวอร์คืนทรัพยากรและไคลเอ็นต์เชื่อมต่อใหม่ได้อย่างรวดเร็ว คุณปฏิบัติ WebSockets & Real-Time Systems with Spring ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน WebSockets & Real-Time Systems with Spring หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน WebSockets & Real-Time Systems with Spring บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “สัญญาณชีพและการคงการเชื่อมต่อด้วย Ping/Pong” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน WebSockets & Real-Time Systems with Spring นี้ได้ไหม
ได้ บทเรียน WebSockets & Real-Time Systems with Spring ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การจัดการข้อผิดพลาดของ WebSocket อย่างเหมาะสม
- การจัดการวงจรชีวิตของการเชื่อมต่อ
- การลองใหม่และกลไกสำรอง
- สัญญาณชีพและการคงการเชื่อมต่อด้วย Ping/Pong