Tech Tip: 4D.WebSocket instantiation and onOpen function sequence
PRODUCT: 4D | VERSION: 20 | PLATFORM: Mac & Win
Published On: January 25, 2024
In v20 R2, when creating a new WebSocket client using 4D.WebSocket.new(), an instance of a WebSocket connection handler user class must be passed as the 2nd parameter. In the WebSocket connection handler class, one of the key functions is the “onOpen” function, which handles logic when the WebSocket client is created and connects to a WebSocket server.
Note that when instantiating a new WebSocket client, the "onOpen" function is called before the 4D.WebSocket object is created. Thus, any subsequent logic should be handled accordingly.
// WSConnectionHandler class Function onOpen // Do something 1st |
// Form method Form.webSocketClient:=4D.WebSocket.new($url; cs.WSConnectionHandler.new()) If (Form.webSocketClient.status="Connected") // Do something 2nd End if |