说明
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); }