我需要註冊一個ColdFusion回調(使用Lucee)將從一個Java類中如下執行:如何註冊ColdFusion回調並從Java類中調用它?
(我掐滅如何我想象調用回調 - 在下面的評論)
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": {
}
});
*/
}
}
創建ColdFusion的「對象」是這樣的(這已經工作):
// create the websocket client
uriObject = createObject("java", "java.net.URI").init("ws://local.websockets");
wsClient = CreateObject("java", "WSClient").init(uriObject);
現在我需要註冊一個回調,我想它會像這樣做:
function void wsCallback (data) {
switch(data.action) {
case "onOpen":
break;
case "onClose":
break;
case "onMessage":
break;
case "onError":
break;
}
};
wsClient.setCallback(wsCallback);
問題是,我該如何做最後一部分(在類中設置回調)?
由於它是一個抽象類,請嘗試使用[CFCProxy](https://helpx.adobe.com/coldfusion/developing-applications/using-web-elements-and-external-objects/integrating-jee-and -java元素合CFML應用/增強-java的集成功能於coldfusion.html)。創建具有所需功能的CFC(「onClose」等等)。然後在你的java類中,使用coldfusion.cfc.CFCProxy創建該組件的一個實例,並調用相應的函數,即'CFCProxy yourInstance = new CFCProxy(cfcPath ...); yourInstance.invoke(「onClose」,args)'。 – Leigh
嗯......我之前沒有用過CFCProxy。我將不得不進行調查。謝謝你的提示! – Redtopia
(編輯)應該很簡單。在CF方面沒什麼特別的。在java中,只需使用'new CFCProxy(cfcPath ...)'和'invoke(...)'創建並創建實例就可以了:) – Leigh