Sending websocket ping/pong frame from browser

前端 未结 3 1476
广开言路
广开言路 2020-11-28 03:03

I keep reading about ping/pong messages in websockets to keep the connection alive, but I\'m not sure what they are. Is it a distinct frame type? (I don\'t see any methods

3条回答
  •  野性不改
    2020-11-28 03:28

    There is no Javascript API to send ping frames or receive pong frames. This is either supported by your browser, or not. There is also no API to enable, configure or detect whether the browser supports and is using ping/pong frames. There was discussion about creating a Javascript ping/pong API for this. There is a possibility that pings may be configurable/detectable in the future, but it is unlikely that Javascript will be able to directly send and receive ping/pong frames.

    However, if you control both the client and server code, then you can easily add ping/pong support at a higher level. You will need some sort of message type header/metadata in your message if you don't have that already, but that's pretty simple. Unless you are planning on sending pings hundreds of times per second or have thousands of simultaneous clients, the overhead is going to be pretty minimal to do it yourself.

提交回复
热议问题