Is it possible to intercept and cache WebSocket messages in a Service Worker like all the examples do for normal HTTP requests?

前端 未结 2 1856
悲哀的现实
悲哀的现实 2021-01-17 09:27

I know you can create WebSocket connections from within a Service Worker itself; my question is more whether or not you can use a WebSocket from your app as normal and have

2条回答
  •  被撕碎了的回忆
    2021-01-17 10:18

    It's not possible for a service worker to intercept Web Socket traffic.

    The service worker's fetch event is triggered only for controlled clients' HTTPS requests, and the message event is triggered only for postMessage() requests from clients.

    While there is an HTTP handshake when initiating a Web Socket connection, that handshake does not trigger a fetch handler, and neither does the actual traffic sent once the connection has been established.

提交回复
热议问题