简体   繁体   English

如何注册ColdFusion回调并从Java类调用它?

[英]How do I register a ColdFusion callback and call it from a Java class?

I need to register a ColdFusion callback (using Lucee) that will be executed from within a Java class as follows: 我需要注册一个ColdFusion回调(使用Lucee),该回调将从Java类中执行,如下所示:

(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): 在ColdFusion中创建“对象”看起来像这样(这已经起作用了):

// 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)? 问题是,我该怎么做最后一部分(在类中设置回调)?

Since it is an abstract class, try using the CFCProxy . 由于它是一个抽象类,请尝试使用CFCProxy To use it create a CFC with the desired functions, ie "onClose", etcetera. 要使用它,请创建具有所需功能(例如“ onClose”等)的CFC。 Then inside your java class, import coldfusion.cfc.CFCProxy and create an instance of that component. 然后在您的Java类中,导入coldfusion.cfc.CFCProxy并创建该组件的实例。 Then invoke the appropriate function, ie 然后调用适当的函数,即

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

The CFCProxy class should already be in the core class path. CFCProxy类应该已经在核心类路径中。 If you are not sure where to find it, try this tip to find out which jar contains that class. 如果不确定在哪里可以找到它,请尝试此技巧以找出包含该类的jar。 Just substitute createObject("java", "coldfusion.cfc.CFCProxy") for the "testClass". 只需将createObject("java", "coldfusion.cfc.CFCProxy")替换为“ testClass”。 I tried it with Lucee 4.5 Express, and it said it is located in {luccee_root}/lib/ext/lucee.jar . 我在Lucee 4.5 Express上尝试过,它说它位于{luccee_root}/lib/ext/lucee.jar

NB: It looks like Lucee also has other (cooler) options for interacting with the CF engine from java . 注意:看起来Lucee还具有其他(更酷的)选项,可用于从java与CF引擎进行交互

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM