Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open and close a sockjs connection from client side?

Tags:

sockjs

Currently, my client side connection opens when I refresh the page and closes when I close/refresh the tab.

I know I can close the connection from server side but I can't find it in the documentation to do so from the client side.

I want to be able to open a new connection on clicking a button, and closing the current connection on clicking another dom button.

How can I open or close a connection from the client side?

I basically want to do this;

    div.col-md-6
        div.btn-group.pull-right
            button#button-newConnection.btn.btn-lg.btn-success(type='button', onclick="newConnection()") New Connection
            button#button-closeConnection.btn.btn-lg.btn-danger(type='button', onclick="closeConnection()") Close Connection
function openConnection () {
    sock.connect();
}

function closeConnection () {
    sock.end();
}

Thank you.

like image 611
Rishav Sharan Avatar asked Sep 02 '25 16:09

Rishav Sharan


1 Answers

Sock.js follows HTML5 Websockets API as closely as possible. Just use .close() on the Sock.js object:

function closeConnection () {
    sock.close();
}
like image 61
franzlorenzon Avatar answered Sep 06 '25 08:09

franzlorenzon