RTCDataChannel: sự kiện lỗi
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since January 2020.
Sự kiện WebRTC error được gửi đến trình xử lý sự kiện onerror của đối tượng onerror khi xảy ra lỗi trên kênh dữ liệu.
Đối tượng RTCErrorEvent cung cấp thông tin chi tiết về lỗi đã xảy ra; xem bài viết đó để biết chi tiết.
Sự kiện này không thể hủy và không bong bóng.
Cú pháp
Sử dụng tên sự kiện trong các phương thức như addEventListener() hoặc đặt thuộc tính xử lý sự kiện.
addEventListener("error", (event) => { })
onerror = (event) => { }
Loại sự kiện
Một RTCErrorEvent. Kế thừa từ Event.
Thuộc tính sự kiện
Ngoài các thuộc tính được liệt kê bên dưới, còn có các thuộc tính từ giao diện gốc, Event.
Ví dụ
// Strings for each of the SCTP cause codes found in RFC
// 4960, section 3.3.10:
// https://datatracker.ietf.org/doc/html/rfc4960#section-3.3.10
const sctpCauseCodes = [
"No SCTP error",
"Invalid stream identifier",
"Missing mandatory parameter",
"Stale cookie error",
"Sender is out of resource (i.e., memory)",
"Unable to resolve address",
"Unrecognized SCTP chunk type received",
"Invalid mandatory parameter",
"Unrecognized parameters",
"No user data (SCTP DATA chunk has no data)",
"Cookie received while shutting down",
"Restart of an association with new addresses",
"User-initiated abort",
"Protocol violation",
];
dc.addEventListener("error", (ev) => {
const err = ev.error;
console.error("WebRTC error: ", err.message);
// Handle specific error detail types
switch (err.errorDetail) {
case "sdp-syntax-error":
console.error(" SDP syntax error in line ", err.sdpLineNumber);
break;
case "idp-load-failure":
console.error(
" Identity provider load failure: HTTP error ",
err.httpRequestStatusCode,
);
break;
case "sctp-failure":
if (err.sctpCauseCode < sctpCauseCodes.length) {
console.error(" SCTP failure: ", err.sctpCauseCode);
} else {
console.error(" Unknown SCTP error");
}
break;
case "dtls-failure":
if (err.receivedAlert) {
console.error(" Received DTLS failure alert: ", err.receivedAlert);
}
if (err.sentAlert) {
console.error(" Sent DTLS failure alert: ", err.receivedAlert);
}
break;
}
// Add source file name and line information
console.error(
" Error in file ",
err.filename,
" at line ",
err.lineNumber,
", column ",
err.columnNumber,
);
});
Sự kiện nhận được cung cấp thông tin chi tiết trong đối tượng RTCError có tên là error; RTCError là phần mở rộng của giao diện DOMException. name của lỗi là RTCError và message là một chuỗi lỗi được chỉ định bởi lớp WebRTC.
Thông tin lỗi được xuất ra bảng điều khiển bằng console.error(). Chuỗi message luôn được xuất ra, cũng như thông tin về tên, số dòng và số cột của tệp nguồn nơi xảy ra lỗi.
Tuy nhiên, ngoài ra, tùy thuộc vào giá trị của errorDetail, thông tin bổ sung có thể được xuất ra. Mỗi loại lỗi có một tập hợp thông tin đầu ra khác nhau. Ví dụ: lỗi cú pháp SDP hiển thị số dòng của lỗi trong SDP và lỗi SCTP hiển thị thông báo tương ứng với mã nguyên nhân SCTP. Các loại lỗi khác cũng đưa ra thông tin phù hợp tương tự.
Bạn cũng có thể thiết lập trình xử lý sự kiện cho các sự kiện error bằng cách sử dụng thuộc tính trình xử lý sự kiện onerror của giao diện RTCDataChannel:
dc.onerror = (ev) => {
const err = ev.error;
// …
};
[!LƯU Ý] Vì
RTCErrorkhông phải là một trong các lỗi cũ nên giá trị củaRTCError.codeluôn là 0.
Thông số kỹ thuật
| Thông số kỹ thuật |
|---|
| WebRTC: Real-Time Communication in Browsers> # event-datachannel-error> |
| WebRTC: Real-Time Communication in Browsers> # dom-rtcdatachannel-onerror> |
Khả năng tương thích của trình duyệt
Xem thêm
- API WebRTC
- Một ví dụ RTCDataChannel đơn giản
- Sự kiện liên quan:
open,messagevàclose