I need to register a ColdFusion callback (using Lucee) that will be executed from within a Java class as follows:
(I stubbed out how I envision invoking the callback - in comments below)
package com.bonnydoonmedia.io;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft_10;
import org.java_websocket.handshake.ServerHandshake;
import java.net.URI;
/*
* author: Robert Munn
* date: 3/15/15
*
* WSClient.java
*
* Simple extension of WebSocketClient by Too Tall Nate
*
* based on example client at
*
* https://github.com/TooTallNate/Java-WebSocket
*
* License: Mozilla Public License 2.0
*
*/
public class WSClient extends WebSocketClient{
public WSClient( URI serverUri , Draft_10 draft ) {
super( serverUri, draft );
}
public WSClient( URI serverURI ) {
super( serverURI );
}
public void connect(){
super.connect();
}
public void send( String message ){
super.send( message );
}
@Override
public void onOpen( ServerHandshake handshakedata ) {
System.out.println( "opened connection" );
System.out.println( "ready state : " + super.getReadyState() );
/* INVOKE THE CALLBACK HERE LIKE:
callback({
"action": "onOpen",
"data": {}
});
*/
}
@Override
public void onMessage( String message ) {
System.out.println( "received: " + message );
/* INVOKE THE CALLBACK HERE LIKE:
callback({
"action": "onMessage",
"data": {
"message": message
}
});
*/
}
@Override
public void onClose( int code, String reason, boolean remote ) {
// The codecodes are documented in class org.java_websocket.framing.CloseFrame
System.out.println( "Connection closed by " + ( remote ? "remote peer" : "us" ) );
/* INVOKE THE CALLBACK HERE LIKE:
callback({
"action": "onClose",
"data": {
}
});
*/
}
@Override
public void onError( Exception ex ) {
ex.printStackTrace();
// if the error is fatal then onClose will be called additionally
/* INVOKE THE CALLBACK HERE LIKE:
callback({
"action": "onMessage",
"data": {
}
});
*/
}
}
Creating the "object" in ColdFusion looks like this (this already works):
// create the websocket client
uriObject = createObject( "java", "java.net.URI" ).init("ws://local.websockets");
wsClient = CreateObject("java", "WSClient").init(uriObject);
Now I need to register a callback, and I'm thinking it would be done like this:
function void wsCallback (data) {
switch(data.action) {
case "onOpen":
break;
case "onClose":
break;
case "onMessage":
break;
case "onError":
break;
}
};
wsClient.setCallback(wsCallback);
The question is, how do I do the last part (setting the callback in the class)?