简体   繁体   English

使用Dropwizard 0.7.0实现长轮询服务器

[英]Implementing long polling server using Dropwizard 0.7.0

I'm trying to implement a long polling server using Dropwizard 0.7.0 framework. 我正在尝试使用Dropwizard 0.7.0框架实现长轮询服务器。 I've been suggested to use jetty integration. 建议我使用码头集成。 After some googling, I got really confused by things like websockets, jetty continuation, cometd. 经过一番谷歌搜索后,我对websockets,码头续航,彗星之类的东西感到非常困惑。

My question is, what are these things and which one should I choose? 我的问题是,这些东西是什么,我应该选择哪一个? And any example is really appreciated! 任何例子都非常感谢!


Edited 已编辑

Our server has many clients, including mobile (ios, android), pc and web. 我们的服务器有许多客户端,包括移动设备(iOS,Android),PC和Web。 Is websocket only available in web browser? websocket仅在Web浏览器中可用吗?

Websocket is available in all the clients you have listed. Websocket在您列出的所有客户端中都可用。 Usually frameworks like Atmoshphere handles downgrading to other types of transports (eg longpolling instead of websockets) and abstracting away the differences for you. 通常,诸如Atmoshphere之类的框架会处理降级为其他类型的传输(例如,使用长轮询而不是websocket)并为您抽象化差异。 Websockets is the standard for things that long-polling tries to solve - ie server side push. Websockets是长轮询尝试解决的问题的标准-即服务器端推送。

I have done websockets on jetty for Dropwizard 0.7.0 - but have a read on the thread I have linked to in the DW google group. 我已经完成了Dropwizard 0.7.0的码头上的websockets-但在DW google组中我链接到的线程上有阅读。

See http://www.eclipse.org/jetty/documentation/9.0.6.v20130930/websockets.html and https://groups.google.com/d/msg/dropwizard-user/doNCx_35urk/5PIvd8_NHIcJ 请参阅http://www.eclipse.org/jetty/documentation/9.0.6.v20130930/websockets.htmlhttps://groups.google.com/d/msg/dropwizard-user/doNCx_35urk/5PIvd8_NHIcJ

Basically you add a websocket-servlet to DW which negotiates a websocket session: 基本上,您将一个websocket-servlet添加到DW中,以协商一个websocket会话:

final ServletRegistration.Dynamic websocket = environment.servlets().addServlet(
            "websocket",
            new MyWebSocketServlet(
                    environment.getObjectMapper(), 
                    environment.metrics(),
                    configuration.getKafkaConfig()
            )
    );
    websocket.setAsyncSupported(true);
    websocket.addMapping("/websocket/*");

And the websocket servlet: 和websocket servlet:

public class MyWebSocketServlet extends WebSocketServlet{

  @Override
  public void configure(WebSocketServletFactory factory) {
    factory.register(MyWebSocketEndpoint.class);
  }
}

And last is your endpoint which is instanciated by the jetty websocket libs: 最后是您的端点,这是通过码头websocket库实现的:

@WebSocket
public class MyWebSocketEndpoint {

    @OnWebSocketMessage
    public void onMessage(Session session, String s) throws IOException {
        session.getRemote().sendString("Returned; "+s);
    }

}

If you want to follow the JSR-356 websockets standard you can use one of these two Dropwizard bundles: 如果要遵循JSR-356网络套接字标准,则可以使用以下两个Dropwizard捆绑软件之一:

I wrote the second one in order to support also websockets metrics (count messages, open sessions, session duration statstics etc...). 我写第二篇文章是为了也支持websockets指标(计数消息,打开的会话,会话持续时间统计信息等)。

Example: 例:

<dependency>
   <groupId>com.liveperson</groupId>
   <artifactId>dropwizard-websocket</artifactId>
   <version>XXX</version>
</dependency>

Then: 然后:

public void initialize(Bootstrap<Configuration> bootstrap) {
   bootstrap.addBundle(new WebsocketBundle(AnnotatedEchoServer.class));
}

@Metered
@Timed
@ExceptionMetered
@ServerEndpoint("/annotated-ws")
public static class AnnotatedEchoServer {
    @OnOpen
    public void myOnOpen(final Session session) throws IOException {
        session.getAsyncRemote().sendText("welcome");
    }

    @OnMessage
    public void myOnMsg(final Session session, String message) {
        session.getAsyncRemote().sendText(message.toUpperCase());
    }

    @OnClose
    public void myOnClose(final Session session, CloseReason cr) {
    }
}

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

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