简体   繁体   English

如何在推送数据时监听

[英]How to listen when data is pushed

I'm using the portal javascript library to interact with my Java sockets. 我正在使用portal javascript库与我的Java套接字进行交互。 I can't seem to determine how to get a request mapped to a method on the below resource. 我似乎无法确定如何将请求映射到下面的资源上的方法。 A socket is being established correctly with @Path("workstation/approval/{uuid}") and then JSON data is being passed through the connection. 正在使用@Path("workstation/approval/{uuid}")正确建立套接字,然后通过连接传递JSON数据。 But on the Java side, how do I map that data push to a method so I can process it? 但是在Java方面, 我如何将数据推送映射到方法,以便我可以处理它?

@Path("workstation/approval/{uuid}")
@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON})
public class WorkstationResource {

    ObjectMapper mapper = new ObjectMapper();

    @Context
    private BroadcasterFactory broadcasterFactory;

    @GET
    @Suspend
    public Broadcastable get(@PathParam("uuid") String uuid) {
        return new Broadcastable(getBroadcaster(uuid));
    }

    private Broadcaster getBroadcaster(String uuid) {
        return broadcasterFactory.lookup(JerseyBroadcaster.class, "workstation/approval/"+uuid, true);
    }

    public String onMessage(String message) throws IOException {
        return mapper.writeValueAsString("This is a test");
    }
}

Salut, 萨吕,

are you using WebSocket or Comet? 你在使用WebSocket或Comet吗? In any case, try adding a @Post annotated method and that will work. 在任何情况下,尝试添加@Post注释方法,这将起作用。 Come to the Atmosphere ML if you have more question, or look at the project sample . 如果您有更多问题,请查看Atmosphere ML ,或查看项目示例

--Jeanfrancois --Jeanfrancois

actually its very simple! 其实很简单! just add another parameter to every method for example, change 只需为每个方法添加另一个参数,例如,更改

public Broadcastable get(@PathParam("uuid") String uuid) {
    return new Broadcastable(getBroadcaster(uuid));
}

to

public Broadcastable get(@PathParam("uuid") String uuid,String postData) {
    return new Broadcastable(getBroadcaster(uuid));
}

and you will have the data passed with the connection in postData parameter 并且您将使用postData参数中的连接传递数据

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

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