java - 如何根据移动应用程序上的事件向 webapp 发送消息

标签 java spring events websocket

当移动应用程序发生事件时,如何将消息发送到 Web 应用程序。两者都使用相同的后端服务器。我使用的是 WebSocket,我能够触发消息。这是正确的方法吗?这是我的代码。

webscoket 处理程序

public class MyHandler extends TextWebSocketHandler {
    @Autowired
    private CommonUtil util;

    @Override
    public void handleTextMessage(WebSocketSession session, TextMessage message) throws IOException, InterruptedException {

        while(true){
            Iterator<String> it1 = util.membership_attendance_list.keySet().iterator();
            while (it1.hasNext()) {
                String key = it1.next();
                String membershipId = util.membership_attendance_list.get(key);
                session.sendMessage(new TextMessage(membershipId));
                util.membership_attendance_list.remove(membershipId);

            }
        }
    }
}

应用程序将与此 api 通信

public class AttendanceController{

@Autowired
    private CommonUtil util;

        @RequestMapping(value = "/attendance", method = RequestMethod.POST, headers = "Accept=application/json")
    public Response saveAttendance(@Valid @RequestBody final AttendanceDto dto)){
        final Response response = new Response();
        // implimentation logic goes here
        util.membership_attendance_list.put(eventParticipantMap.getMemberShipId(),eventParticipantMap.getMemberShipId());
        return response;
    }

}

是否可以使用监听器来实现它?

最佳答案

因此,如果我们谈论的是基于移动应用程序的事件。我将给出一个基于 android 的示例,但可以在其他平台上找到类似的概念。某些事件监听器具有针对每种类型事件的回调方法。通常,您需要覆盖监听器接口(interface)或抽象类的抽象回调方法,并在那里注册您的方法行为,以便在每次事件发生时进行 HTTP POST/GET/SOCKET_SEND。

我熟悉基于 Android 的应用程序。所以这里有一个相同的例子:

事件监听器和回调方法

公共(public)类 EventExampleActivity 扩展 Activity {

@Override
protected void onStart()
{
        super.onStart();

        Button button = (Button)findViewById(R.id.myButton);

        //for listening simple click events
        button.setOnClickListener(
                //registering callback method behaviour here. 
                new Button.OnClickListener() {
                    public void onClick(View v) {
                     // the data related to the events can be fetched
                     // in the v object
                     // insert http get/post/socket_send logic here
                }
            }
        );

        //for listening long click events
        button.setOnLongClickListener(
            new Button.OnLongClickListener() {
                //registering callback method behaviour here.
                public boolean onLongClick(View v) {
                 // the data related to the events can be fetched
                 // in the v object
                 // insert http get/post/socket_send logic here
                return true;
            }
        }
    );

    }
}

希望对您有所帮助。

有关事件监听器的更多信息。 https://guides.codepath.com/android/Basic-Event-Listeners https://www.tutlane.com/tutorial/android/android-input-events-event-listeners-event-handling

关于java - 如何根据移动应用程序上的事件向 webapp 发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50922829/

相关文章:

Spring AOP : Disadvantages when using it - Spring Features which use Spring AOP do not have this disadvantages?

java - 应用@EnableCircuitBreaker后获取异常java.lang.ArrayStoreException : sun. Reflect.annotation.TypeNotPresentExceptionProxy

javascript - onmouseover() 在 1 秒后调用 onclick?

c# - 为什么 SelectionChanged 会从 ComboBox 冒泡到父 TabControl?

java - 隐藏打印对话框

java - 重构 - 在 Netbeans 8.0.2 中提取方法对象

java - 当一个类被加载到 JVM 中时

java - Java 编译器通常会预先计算最终字段的哈希码吗?

java - CustomDeserializer 没有默认(无参数)构造函数

javascript - Angular 5 keyup 事件被触发两次