Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 248
Fricken Hamster Avatar asked Jul 10 '15 23:07

Fricken Hamster


People also ask

How do you initialize a WebSocket?

All you have to do is call the WebSocket constructor and pass in the URL of your server. // Create a new WebSocket. var socket = new WebSocket('ws://echo.websocket.org'); Once the connection has been established, the open event will be fired on your Web Socket instance.

What is the difference between WS and WSS?

The wss protocol establishes a WebSocket over an encrypted TLS connection, while the ws protocol uses an unencrypted connection. At this point, the network connection remains open and can be used to send WebSocket messages in either direction.

How do I create a WebSocket room?

WebSocket Server We need to create our first file (index. js) to use the package. Inside the file, we will first import ws and then use it to spin up a WebSocket server. After that, we can start the basic server with npm run start .


1 Answers

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 connection 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.

like image 64
jfriend00 Avatar answered Sep 30 '22 12:09

jfriend00