使用Dropwizard 0.7.0实现长轮询服务器 [英] Implementing long polling server using Dropwizard 0.7.0

查看:145
本文介绍了使用Dropwizard 0.7.0实现长轮询服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Dropwizard 0.7.0框架实现一个长轮询服务器。我被建议使用码头集成。经过一些谷歌搜索,我真的很困惑像websockets,jetty continuation,cometd。

I'm trying to implement a long polling server using Dropwizard 0.7.0 framework. I've been suggested to use jetty integration. After some googling, I got really confused by things like websockets, jetty continuation, cometd.

我的问题是,这些东西是什么,我应该选择哪一个?非常感谢任何一个例子!

My question is, what are these things and which one should I choose? And any example is really appreciated!

已编辑

我们的服务器有很多客户端,包括移动(ios,android),个人电脑和网络。 websocket仅在Web浏览器中可用吗?

Our server has many clients, including mobile (ios, android), pc and web. Is websocket only available in web browser?

推荐答案

Websocket在您列出的所有客户端中都可用。
通常像Atmoshphere这样的框架会处理降级到其他类型的传输(例如longpolling而不是websockets)并为你抽象出差异。 Websockets是长轮询试图解决的问题的标准 - 即服务器端推送。

Websocket is available in all the clients you have listed. Usually frameworks like Atmoshphere handles downgrading to other types of transports (e.g. longpolling instead of websockets) and abstracting away the differences for you. Websockets is the standard for things that long-polling tries to solve - i.e. server side push.

我在jetty上为Dropwizard 0.7.0完成了websockets - 但是有一个读取在我链接到DW google组的线程上。

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.

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

基本上你将一个websocket-servlet添加到DW来协商一个websocket会话:

Basically you add a websocket-servlet to DW which negotiates a websocket session:

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

和websocket servlet:

And the websocket servlet:

public class MyWebSocketServlet extends WebSocketServlet{

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

最后是您的码头由码头实现websocket libs:

And last is your endpoint which is instanciated by the jetty websocket libs:

@WebSocket
public class MyWebSocketEndpoint {

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

}

这篇关于使用Dropwizard 0.7.0实现长轮询服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆