Closing WebSocket correctly (HTML5, Javascript)

后端 未结 5 1892
悲哀的现实
悲哀的现实 2020-11-28 02:13

I am playing around with HTML5 WebSockets. I was wondering, how do I close the connection gracefully? Like, what happens if user refreshes the page, or just closes the brows

5条回答
  •  借酒劲吻你
    2020-11-28 03:12

    By using close method of web socket, where you can write any function according to requirement.

    var connection = new WebSocket('ws://127.0.0.1:1337');
        connection.onclose = () => {
                console.log('Web Socket Connection Closed');
            };
    

提交回复
热议问题