Dan Pahnke · Jul 23, 2019

Server event from browser websocket connection close?


I've got a csp page that has successfully set up a websocket connection to a Cache class. When the websocket object's close function is triggered at the browser I was expecting one of the class' methods to fire at the server. The onclose method is triggered at the browser but nothing at the server. Is a method supposed to fire at the server?




Cache for Windows (x86-64) 2015.1.4 (Build 803_6) Tue May 15 2018 12:08:36 EDT

0 4 331 2


The OnPostServer() method of your WebSocket class should be triggered when the WebSocket closes, provided that you return from the Server() method when that happens.

Thanks Aohan but still nothing.

At the browser I trigger ws.close()

At the server the web socket class' Server method executes quitting with $$$OK the  OnPostServer method does not execute.

Closing the connection from the server end by invoking the web socket object's EndServer method results in the OnPostServer method executing.

Maybe of importance, the websocket is asynchronous so when triggered from the browser's .close() the Server method is not invoking the EndServer method.

I see. In asynchronous mode, you do have to call EndServer() explicitly to get the OnPostServer() method to execute. This means that you'll have to handle the client closure within your class. One way to do that is to have the client send a special message shortly before closing to indicate that a disconnect is about to happen. You can then handle that message in your WebSocket server class. Another way is to do a Read() and handle the case where $$$CSPWebSocketClosed is returned.

Thanks again Aohan.

I took a different approach. I overrode the .close() method at the browser with one that uses #server to send the "I'm closing" message to the Cache server. The message handler at the server then executes the EndServer method in the web socket class.