简体   繁体   中英

Create websocket without connecting

Is there a way to create a websocket without connecting to it right away? So far I think

var ws = new WebSocket("ws://ws.my.url.com");

creates and then connects right away.

I'd like to create the socket, define the callbacks, and then connect once the user clicks a connect button.

Thanks

No. Creating a webSocket means you've created a connection to a server (or started the process of connecting). There is no such thing as creating a webSocket that isn't connecting yet.

You could create an object that would store all the configuration parameters and then just tell that object to connect when you wanted to, though I'm not really sure why that's better than just creating the actual webSocket when you the connect to be made.

Or just create a function that does all the setup work and call that function later when the user clicks a connect button.

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