简体   繁体   English

无需连接即可创建websocket

[英]Create websocket without connecting

Is there a way to create a websocket without connecting to it right away? 有没有办法创建一个websocket而不立即连接它? 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). 否。创建webSocket意味着您已创建与服务器的连接(或启动连接过程)。 There is no such thing as creating a webSocket that isn't connecting yet. 没有创建一个尚未连接的webSocket。

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. 您可以创建一个存储所有配置参数的对象,然后在需要时告诉该对象进行连接,尽管我不确定为什么这比在创建连接时创建实际的webSocket要好。

Or just create a function that does all the setup work and call that function later when the user clicks a connect button. 或者只是创建一个执行所有设置工作的函数,并在用户单击连接按钮后调用该函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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