连接错误
你可以通过在 connection
事件监听器的根作用域中抛出错误来阻止拦截的 WebSocket 客户端连接。
¥You can error the intercepted WebSocket client connection by throwing an error at the root scope of your connection
event listener.
api.addEventListener('connection', () => {
throw new Error('Failed to connect')
})
这将导致在你的 WebSocket 客户端上分派 error
事件,并使用 1011
闭包代码关闭底层连接(如果尚未关闭)。此外,抛出的错误将被记录到控制台以供参考。
¥This will result in the error
event being dispatched on your WebSocket client and the underlying connection being closed with the 1011
closure code, if it hasn’t been already. Additionally, the thrown error will be logged to the console for reference.
请注意,根据 WHATWG WebSocket API 规范,你无法在客户端的
error
事件监听器中访问确切的错误参考或任何有关错误的信息。¥Note that you cannot access the exact error reference or any information about the error in the
error
event listener on the client as per WHATWG WebSocket API specification.