如何根据移动应用上的活动向webapp发送消息

Sha*_* PC 7 java events spring 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);

            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

应用程序将与此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;
    }

}
Run Code Online (Sandbox Code Playgroud)

是否有可能使用听众来表达它?

Pre*_*mar 0

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

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

事件监听器和回调方法

公共类 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;
            }
        }
    );

    }
}
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你。

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