Java 自定义事件处理程序和侦听器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33948916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:10:53  来源:igfitidea点击:

Java custom event handler and listeners

javaeventssocket.ionettylisteners

提问by Tony

I'm currently playing around with a Java implementation of Socket.io, available here: netty-socketio

我目前正在使用 Socket.io 的 Java 实现,可在此处获得:netty-socketio

I've got the server up and running, and its receiving/sending messages nicely between client and server, but I need to have events trigger on certain messages being received, and that's where I'm confused.

我已经启动并运行服务器,并且它在客户端和服务器之间很好地接收/发送消息,但是我需要在接收到某些消息时触发事件,这就是我感到困惑的地方。

Here's my code:

这是我的代码:

server.addEventListener("message", clientData.class, new DataListener<clientData>() {
        @Override
        public void onData(SocketIOClient client, clientData data, AckRequest ackRequest) throws Exception {
            System.out.println("Message from client: " + data.getMessage());

        }

    });



public class clientData{

String message;

public String getMessage() {
    return message;
}

public void setMessage(String message) {
    this.message = message;
}

}

}

Essentially what I'd like to happen is when a particular message is received from a client, I need a function within another class to run. I've spent the last two hours reading about Observable, Observer, Interfacesand event handlers, but I'm really not sure how to set this up.

基本上我想要发生的是当从客户端接收到特定消息时,我需要另一个类中的函数来运行。我花了持续两个小时阅读ObservableObserverInterfaces和事件处理程序,但我真的不知道如何处理此问题。

The library also makes mention of this DataListener, but I've no idea what that is, as there's little documentation in the library.

图书馆也提到了 this DataListener,但我不知道那是什么,因为图书馆里的文档很少。

Any input or advice on this would be greatly appreciated.

对此的任何意见或建议将不胜感激。

Thanks

谢谢

采纳答案by Sweeper

Let's say your class that raises the event is called A. And the class that needs to listen for the event is called B. And the event is called SomeEvent.

假设您引发事件的类称为A。而需要监听事件的类被称为B. 并且该事件被称为SomeEvent

First, create an interface called SomeEventListener:

首先,创建一个名为 的接口SomeEventListener

public interface SomeEventListener {
    void onSomeEvent ();
}

If there are arguments that you want to pass when the event occurs (typically something about the event), you can add it to the method.

如果您想在事件发生时传递参数(通常是关于事件的一些内容),您可以将其添加到方法中。

Then in A, you add a field:

然后在 中A,添加一个字段:

private SomeEventListener listener;

and a method:

和一个方法:

public void setSomeEventListener (SomeEventListener listener) {
    this.listener = listener;
}

This way, Bcan call setSomeEventListenerto set the listener.

这样,B就可以调用setSomeEventListener设置监听器了。

When the event occurs, Ashould call

当事件发生时,A应该调用

if (listener != null) listener.onSomeEvent ();

And that's all there is to A!

这就是全部A

In B, you need to implement the interface:

B,需要实现接口:

public class B implements SomeEventListener {
    public void onSomeEvent () {
        //do whatever you like when SomeEvent happens.
    }
}

And you can listen for SomeEventlike this:

你可以这样听SomeEvent

someInstanceOfA.setSomeEventListener (this);

And after this call, all the SomeEventraised by Acan be listened by B!

在这个电话之后,所有SomeEvent提出的A都可以被听到B

Using the Observable and Observer pattern, we can see that Ais an Observable and Bis an Observer.

使用 Observable 和 Observer 模式,我们可以看到它A是一个 Observable 并且B是一个观察者。

That's easy!

这很容易!