If you are using b4j_ws.js, then open it up in Notepad or something and add this to the bottom, just before the last closing bracket:
b4j_ws.onclose = function(event)
{
b4j_runFunction("socketDisconnected", event);
};
And then in your webpage (the one that has <script src="b4j_ws.js"> in it), make sure to implement the "socketDisconnected" function. For example:
function socketDisconnected(event)
{
alert("Websocket connection closed");
}