xemjas
xemjas

Reputation: 135

Create a new connection of websocket from an opened one

I have a websocket that is created using this statement :

ws = new WebSocket(targetURL);

and my javascript runs all the function inside ws.onopen (which is only an alert, stating that the ws is opened).

However, after 15 minutes or so, I want to create a new connection of websocket using the same variable ws. I tried this again :

ws = new WebSocket(targetURL);

But this time, it does not look like that the ws.onopen function is not run (there is no alert stating that the ws is opened). So I assume that that statement cannot be used to create a new connection, can it?

Is there a way of doing it?

Thank you

Upvotes: 0

Views: 72

Answers (1)

vtortola
vtortola

Reputation: 35905

Since you are creating a new object you must at attach an event handler. Handlers are associated with objects, not variables. Write your logic in a separated function, and assign that function to "ws.onopen" after creation.

Upvotes: 1

Related Questions