Tomcat和Jetty對WebSocket的支援

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   io   os   ar   java   

公司項目需要,瞭解了下目前幾種支援WebSocket的架構。以前用jWebSocket做過一些項目,相對來說,改jWebSocket的源碼略複雜,也不是一天兩天能搞定的。一調研才發現,現在很多主流的web架構都已經開始支援WebSocket了,不得不感慨時間太快,科技進步太快,在微策略的幾年真的荒廢了。不多說,先記錄下今天的研究。


Tomcat:

       J2EE下面用的最多的容器應該就是tomcat了。說到tomcat對WebSocket的支援,不得不先提一下,目前的WebSocket協議已經經過了好幾代的演變,不同瀏覽器對此協議的支援程度也不同,因此,如果作為伺服器,最理想的是支援儘可能多的WebSocket協議版本。

tomcat8真正支援jsr-356(包含對websocket的支援), tomcat7支援部分版本的websocket實現不相容jsr-356。因此,能用tomcat8的話,還是盡量用。


代碼實現相當簡單,以下是一個列子,只需要tomcat8的基本庫,不需要其他依賴。

import java.io.IOException;  import javax.websocket.OnClose;  import javax.websocket.OnMessage;  import javax.websocket.OnOpen;  import javax.websocket.Session;  import javax.websocket.server.ServerEndpoint;  @ServerEndpoint("/websocket")  public class WebSocketTest {        @OnMessage      public void onMessage(String message, Session session) throws IOException,              InterruptedException {          // Print the client message for testing purposes          System.out.println("Received: " + message);          // Send the first message to the client          session.getBasicRemote().sendText("This is the first server message");          // Send 3 messages to the client every 5 seconds          int sentMessages = 0;          while (sentMessages < 3) {              Thread.sleep(5000);              session.getBasicRemote().sendText("This is an intermediate server message. Count: " + sentMessages);              sentMessages++;          }          // Send a final message to the client          session.getBasicRemote().sendText("This is the last server message");      }        @OnOpen      public void onOpen() {          System.out.println("Client connected");      }        @OnClose      public void onClose() {          System.out.println("Connection closed");      }  }  


Jetty:

       Jetty和Tomcat一樣,也是一個Servlet的容器。如果說不同之處,那麼最大的不同應該是Tomcat採用的是BIO處理方式,也就是說一個request會用一個線程去處理,即使是WebSocket這種長串連,也是會獨立開一個線程。作為一個普通的Web伺服器,tomcat可以輕鬆應對耗時比較短的Request/Response。但是如果換成是長串連的WebSocket,那麻煩就來了,對於上萬使用者的聊天和推送,總不能開上萬個線程去處理吧。此時,Jetty的效能就體現出來了,Jetty採用的是NIO,一個線程可以處理多個WebSocket的長連結,如果你的需求是大量耗時比較長的request或者大量長串連,那麼建議採用Jetty。


        Jetty對WebSocket的實現有點繞,Servlet不再是繼承原來的HttpServlet,而是繼承WebSocketServlet。此處要注意匯入jetty-util.jar和jetty-websocket.jar兩個包,否則可能會有class not found錯誤。

ReverseAjaxServlet.java:

import java.io.IOException;import java.util.Date;import java.util.Random; import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse; import org.codehaus.jettison.json.JSONArray;import org.eclipse.jetty.websocket.WebSocket;import org.eclipse.jetty.websocket.WebSocketServlet; /** * @author Mathieu Carbou ([email protected]) */public final class ReverseAjaxServlet extends WebSocketServlet {     private final Endpoints endpoints = new Endpoints();    private final Random random = new Random();    private final Thread generator = new Thread("Event generator") {        @Override        public void run() {            while (!Thread.currentThread().isInterrupted()) {                try {                    Thread.sleep(random.nextInt(5000));                    endpoints.broadcast(new JSONArray().put("At " + new Date()).toString());                } catch (InterruptedException e) {                    Thread.currentThread().interrupt();                }            }        }    };     @Override    public void init() throws ServletException {        super.init();        generator.start();    }     @Override    public void destroy() {        generator.interrupt();        super.destroy();    }         @Override    public WebSocket doWebSocketConnect(HttpServletRequest request, String protocol) {        return endpoints.newEndpoint();    }     @Override    protected void doGet(HttpServletRequest req, HttpServletResponse resp)            throws ServletException, IOException {        resp.getWriter().write("11111");    }}

Endpoints.java:

package com.cn.test.chapter2.websocket;import org.eclipse.jetty.websocket.WebSocket;import java.util.Queue;import java.util.concurrent.ConcurrentLinkedQueue; /** * @author Mathieu Carbou ([email protected]) */final class Endpoints {    private final Queue<Endpoint> endpoints = new ConcurrentLinkedQueue<Endpoint>();     void broadcast(String data) {//        for (Endpoint endpoint : endpoints) {//            endpoint.onMessage(data);//        }    }     void offer(Endpoint endpoint) {        endpoints.offer(endpoint);    }     void remove(Endpoint endpoint) {        endpoints.remove(endpoint);    }     public WebSocket newEndpoint() {        return new Endpoint(this);    }}

Endpoint.java

import java.io.IOException;import java.util.concurrent.ConcurrentLinkedQueue; import org.codehaus.jettison.json.JSONArray;import org.eclipse.jetty.websocket.WebSocket; /** * @author Mathieu Carbou ([email protected]) */class Endpoint implements WebSocket.OnTextMessage  {     protected Connection _connection;         private Endpoints endpoints;         private static int clientCounter = 0;    private int clientId = clientCounter++;         public Endpoint(Endpoints endpoints) {        this.setEndpoints(endpoints);    }         @Override    public void onClose(int code, String message) {    System.out.println("Client disconnected");              this.endpoints.remove(this);    }     @Override    public void onOpen(Connection connection) {    System.out.println("Client connected");          _connection = connection;        try {            this._connection.sendMessage(new JSONArray().put("ClientID = " + clientId).toString());        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        endpoints.offer(this);    }     @Override    public void onMessage(final String data) {    System.out.println("Received data: " + data);          this.endpoints.broadcast(data);    }     public Endpoints getEndpoints() {        return endpoints;    }     public void setEndpoints(Endpoints endpoints) {        this.endpoints = endpoints;    }}




協助工具輔助:

        在編寫伺服器最麻煩的是要寫對應的用戶端來測試,還好Chrome為我們解決了這個問題。下載Chrome外掛程式WebSocket Clinet可以輕鬆地和伺服器建立串連,發送訊息到伺服器。

Tomcat和Jetty對WebSocket的支援

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.