简体   繁体   中英

Is it possible to send a ping opcode using javascript Websocket?

I have a websocket client and I want it to send a ping frame to my WS server. According to the RFC 6455 a ping frame is represented by opcode %x9 but I don't know if it's even possible to send from a browser.

Any help will be appreciated

Building on-top of the comments in your original post, you can manually send pings via websocket.

The link by Daniel W. of the ws library has a ping() method which can be called from a valid websocket client. See an example here .

If you have written your own websocket library then you must conform to the standard framing outlined in RFC6455 The Websocket Protocol .

You should be able to pack a buffer with the correct header and control opcode and send it over the upgraded HTTP1.1 TCP connection.

Edit:
Further research shows, at the point of writing, the Ping method is not supported via the nativeWebsocket Client and the send method seems to only support payloads and not command messages, which include opcodes for commands such as ping.

An option could be to go with a WASM based solution which could make non-XHR or non-fetch based TCP connections, however this comes with a whole different set of challenges.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM