cometd源碼閱讀-WebSocketTransport初始化(五) cometd源碼閱讀-初始化(二)

說明

comted的websocket實現是使用jetty的文檔地址:jetty文檔

Transprot初始化時機參考cometd源碼閱讀-初始化(二) <1>處 會調用transport 的init方法

org.cometd.server.BayeuxServerImpl#initializeServerTransports

protected void initializeServerTransports() {
        if (_transports.isEmpty()) {
            //初始化Transport 沒重定義則創建默認 指定了則創建指定的 注:反射創建 會傳入bayeux
            String option = (String)getOption(TRANSPORTS_OPTION);
            if (option == null) {
                //未定義則初始化處理websocket 和長輪詢的Transport處理器 JSONP的處理器
                // Order is important, see #findHttpTransport()
                //
                ServerTransport transport = newWebSocketTransport();
                if (transport != null) {
                    addTransport(transport);
                }
                addTransport(newJSONTransport());
                addTransport(new JSONPTransport(this));
            } else {
                //如果有進行類的全名稱配置 根據累的全名稱創建
                for (String className : option.split(",")) {
                    ServerTransport transport = newServerTransport(className.trim());
                    if (transport != null) {
                        addTransport(transport);
                    }
                }

                if (_transports.isEmpty()) {
                    throw new IllegalArgumentException("Option '" + TRANSPORTS_OPTION +
                            "' does not contain a valid list of server transport class names");
                }
            }
        }

        //如果沒有配置_allowedTransports 將transport加入到 _allowedTransports//liqiangtodo 暫時不曉得幹嘛的
        if (_allowedTransports.isEmpty()) {
            String option = (String)getOption(ALLOWED_TRANSPORTS_OPTION);
            if (option == null) {
                _allowedTransports.addAll(_transports.keySet());
            } else {
                for (String transportName : option.split(",")) {
                    if (_transports.containsKey(transportName)) {
                        _allowedTransports.add(transportName);
                    }
                }

                if (_allowedTransports.isEmpty()) {
                    throw new IllegalArgumentException("Option '" + ALLOWED_TRANSPORTS_OPTION +
                            "' does not contain at least one configured server transport name");
                }
            }
        }

        //逐個調用transport init方法完成Transport的初始化 Transport 內部的相關參數自定義配置可以通過Option拿到
        List<String> activeTransports = new ArrayList<>();
        for (String transportName : _allowedTransports) {
            ServerTransport serverTransport = getTransport(transportName);
            if (serverTransport instanceof AbstractServerTransport) {
                //調用init方法進行初始化<1>
                ((AbstractServerTransport)serverTransport).init();
                //加入到已激活的transpor
                activeTransports.add(serverTransport.getName());
            }
        }
        if (_logger.isDebugEnabled()) {
            _logger.debug("Active transports: {}", activeTransports);
        }
    }

源碼

<1>

org.cometd.server.websocket.javax.WebSocketTransport#init


//配置的websoket連接地址可參考點擊跳轉>
public static
final String COMETD_URL_MAPPING_OPTION = "cometdURLMapping";
public static final String IDLE_TIMEOUT_OPTION = "idleTimeout";
@Override
    public void init() {
//<2>先調用父類的init方法
super.init(); ServletContext context = (ServletContext)getOption(ServletContext.class.getName()); if (context == null) { throw new IllegalArgumentException("Missing ServletContext"); } String cometdURLMapping = (String)getOption(COMETD_URL_MAPPING_OPTION); if (cometdURLMapping == null) { throw new IllegalArgumentException("Missing '" + COMETD_URL_MAPPING_OPTION + "' parameter"); } /** * org.eclipse.jetty.websocket.jsr356.server.deploy.WebSocketServerContainerInitializer.initialize * jetty的webSocket模塊 */ ServerContainer container = (ServerContainer)context.getAttribute(ServerContainer.class.getName()); if (container == null) { throw new IllegalArgumentException("Missing WebSocket ServerContainer"); } // JSR 356 does not support a input buffer size option //從option配置獲取最大接收文本的緩衝區大小 從option獲取 int maxMessageSize = getMaxMessageSize(); if (maxMessageSize < 0) { maxMessageSize = container.getDefaultMaxTextMessageBufferSize(); } container.setDefaultMaxTextMessageBufferSize(maxMessageSize); //從option獲取在多久沒收收到輸入後鏈接關閉 long idleTimeout = getOption(IDLE_TIMEOUT_OPTION, container.getDefaultMaxSessionIdleTimeout()); container.setDefaultMaxSessionIdleTimeout(idleTimeout); String protocol = getProtocol(); List<String> protocols = protocol == null ? null : Collections.singletonList(protocol); Configurator configurator = new Configurator(context); //根據configurator 自定義配置處理器用於設置websocket mapping 以及接收連接 關閉 消息 等事件 org.cometd.server.websocket.javax.WebSocketEndPoint for (String mapping : normalizeURLMapping(cometdURLMapping)) { ServerEndpointConfig config = ServerEndpointConfig.Builder.create(WebSocketEndPoint.class, mapping) .subprotocols(protocols) .configurator(configurator) .build(); try { container.addEndpoint(config); } catch (DeploymentException x) { throw new RuntimeException(x); } } }

<2>

org.cometd.server.websocket.common.AbstractWebSocketTransport#init

    public static final String PROTOCOL_OPTION = "protocol";
    public static final String MESSAGES_PER_FRAME_OPTION = "messagesPerFrame";
    public static final String REQUIRE_HANDSHAKE_PER_CONNECTION_OPTION = "requireHandshakePerConnection";
    @Override
    public void init() {
        //<3>調用父類的init方法
        super.init();
        _protocol = getOption(PROTOCOL_OPTION, null);
        _messagesPerFrame = getOption(MESSAGES_PER_FRAME_OPTION, 1);
        _requireHandshakePerConnection = getOption(REQUIRE_HANDSHAKE_PER_CONNECTION_OPTION, false);
    }

<3>

org.cometd.server.AbstractServerTransport#init

public static final String TIMEOUT_OPTION = "timeout";
public static final String INTERVAL_OPTION = "interval";
public static final String MAX_INTERVAL_OPTION = "maxInterval";
public static final String MAX_PROCESSING_OPTION = "maxProcessing";
public static final String MAX_LAZY_TIMEOUT_OPTION = "maxLazyTimeout";
public static final String META_CONNECT_DELIVERY_OPTION = "metaConnectDeliverOnly";
public static final String MAX_QUEUE_OPTION = "maxQueue";
public static final String JSON_CONTEXT_OPTION = "jsonContext";
public static final String HANDSHAKE_RECONNECT_OPTION = "handshakeReconnect";
public static final String ALLOW_MESSAGE_DELIVERY_DURING_HANDSHAKE = "allowMessageDeliveryDuringHandshake";
 /**
     * Initializes the transport, resolving default and direct options.
     *初始化傳輸,解析默認和直接選項。
     */
    public void init() {
        _interval = getOption(INTERVAL_OPTION, _interval);
        _maxInterval = getOption(MAX_INTERVAL_OPTION, _maxInterval);
        _timeout = getOption(TIMEOUT_OPTION, _timeout);
        _maxLazyTimeout = getOption(MAX_LAZY_TIMEOUT_OPTION, _maxLazyTimeout);
        _metaConnectDeliveryOnly = getOption(META_CONNECT_DELIVERY_OPTION, _metaConnectDeliveryOnly);
        _jsonContext = (JSONContextServer)getOption(JSON_CONTEXT_OPTION);
        _handshakeReconnect = getOption(HANDSHAKE_RECONNECT_OPTION, false);
        _allowHandshakeDelivery = getOption(ALLOW_MESSAGE_DELIVERY_DURING_HANDSHAKE, false);
        _maxMessageSize = getOption(MAX_MESSAGE_SIZE_OPTION, -1);
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章