Java自定义事件处理程序和侦听器 [英] Java custom event handler and listeners
问题描述
我目前正在使用Socket.io的Java实现,可在此处获取: netty-socketio
I'm currently playing around with a Java implementation of Socket.io, available here: 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.
这是我的代码:
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;
}
}
基本上我想要发生的是当从客户端收到特定消息时,我需要在另一个类中运行一个函数。我花了最近两个小时阅读 Observable
, Observer
,接口
和事件处理程序,但我真的不知道如何设置它。
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
, Interfaces
and event handlers, but I'm really not sure how to set this up.
该库还提到了这个 DataListener
,但是我不知道那是什么,因为那里几乎没有库中的文档。
The library also makes mention of this DataListener
, but I've no idea what that is, as there's little documentation in the library.
对此的任何意见或建议将不胜感激。
Any input or advice on this would be greatly appreciated.
谢谢
推荐答案
假设您提升事件的类名为 A
。需要监听事件的类称为 B
。该事件被称为 SomeEvent
。
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
.
首先,创建一个名为 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.
然后在 A
中添加一个字段:
Then in A
, you add a field:
private SomeEventListener listener;
和方法:
public void setSomeEventListener (SomeEventListener listener) {
this.listener = listener;
}
这样, B
可以调用 setSomeEventListener
来设置监听器。
This way, B
can call setSomeEventListener
to set the listener.
当事件发生时, A
应该调用
When the event occurs, A
should call
if (listener != null) listener.onSomeEvent ();
这就是 A
!
在 B
中,您需要实现界面:
In B
, you need to implement the interface:
public class B implements SomeEventListener {
public void onSomeEvent () {
//do whatever you like when SomeEvent happens.
}
}
你可以听 SomeEvent
是这样的:
someInstanceOfA.setSomeEventListener (this);
此次通话后,所有 SomeEvent
由 A
筹集的 B
!
And after this call, all the SomeEvent
raised by A
can be listened by B
!
使用Observable和Observer模式,我们可以看到 A
是一个Observable而 B
是一个Observer。
Using the Observable and Observer pattern, we can see that A
is an Observable and B
is an Observer.
这很简单!
这篇关于Java自定义事件处理程序和侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!