2

我需要注册一个 ColdFusion 回调(使用 Lucee),它将在 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);

问题是,我该如何做最后一部分(在类中设置回调)?

4

1 回答 1

0

由于它是一个抽象类,请尝试使用CFCProxy。要使用它,请创建一个具有所需功能的 CFC,例如“onClose”等。然后在您的 java 类中,导入coldfusion.cfc.CFCProxy并创建该组件的实例。然后调用相应的函数,即

CFCProxy yourInstance = new CFCProxy("c:/path/to/yourComponent.cfc");
yourInstance.invoke( "onClose", args );

CFCProxy 类应该已经在核心类路径中。如果您不确定在哪里可以找到它,请尝试使用此提示找出哪个 jar 包含该类。只需替换createObject("java", "coldfusion.cfc.CFCProxy")“testClass”即可。我用 Lucee 4.5 Express 试了一下,它说它位于{luccee_root}/lib/ext/lucee.jar.

注意:看起来 Lucee 也有其他(更酷的)选项用于与 java 的 CF 引擎交互

于 2016-10-04T07:30:37.530 回答