Apache Tomcat 8 WebSocket How-To 中文翻译版


Apache Tomcat 8


WebSocket How-To

内容导航

概述

Tomcat提供对在RFC 6455定义的WebSocket的支持。

应用程序开发

Tomcat实现在JSR-356中定义的Java WebSocket 1.0 API。

有几个示例应用程序,演示了如何使用WebSocket API。你需要看看客户端的HTML和服务器端的代码

生产使用

虽然WebSocket实现可以与任何HTTP连接器一起工作,但是不推荐WebSocket和BIO HTTP连接器配合使用。因为WebSocket的典型使用并不适合HTTP BIO连接器,HTTP BIO 连接器要求为每个连接分配一个线程,而不管是否有空闲的连接。

据报告(56304),Linux会花费大量时间来报告丢掉的连接。当以BIO HTTP连接器使用WebSocket时,这可能会导致该时段内写入出现阻塞。这似乎是不可取的。通过使用内核网络参数/proc/sys/net/ipv4/tcp_retries2,可以减少因连接丢失所花费的报告时间。此外,他们使用了非阻塞IO,可以让Tomcat实现自己的超时机制从而处理这些情况,你也可以使用其他的HTTP连接器。

Tomcat WebSocket具体的配置

Tomcat为WebSocket提供了一些Tomcat特定的配置选项。随着时间的推移,预计这些将被收录到WebSocket规范中。当在阻塞模式下发送WebSocket信息时,使用的写入超时时间默认为20000毫秒(20秒)。你可以通过在用户属性集合中设置org.apache.tomcat.websocket.BLOCKING_SEND_TIMEOUT来进行变更,用户属性集合被附加到WebSocket 会话中。

分配给该属性的值应该是一个Long类型的数据,并以毫秒为单位来表示使用的超时时间。如果不限制超时时间,请使用-1。

如果应用程序没有为输入的二进制信息定义一个MessageHandler.Partial,任何输入的二进制信息都应该被缓冲,这样,整个消息可以在一次调用中传递给为二进制信息注册的MessageHandler.Whole。二进制信息默认的缓存大小为8192字节(8KB)。你也可以通过为servlet context的初始化参数org.apache.tomcat.websocket.binaryBufferSize设置期望的以字节为单位的值来更改它。

Java WebSocket 规范 1.0不允许在第一个终端已经开始一个WebSocket握手之后进行规划部署。在默认情况下,Tomcat继续允许额外的规划部署。该行为可以通过servlet context的初始化参数org.apache.tomcat.websocket.noAddAfterHandshake来控制。

你可以通过设置系统属性org.apache.tomcat.websocket.STRICT_SPEC_COMPLIANCEtrue来修改默认值,但是servlet context中的任何显示设置将始终被优先采用。

Java WebSocket 1.0 规范要求在一个不同的线程上执行到发起写入的线程的异步写入回调。由于容器线程池不是通过Servlet API被暴露,因此WebSocket实现必须提供自己的线程池。该线程池可以通过下列servlet context初始化参数被控制:

  • org.apache.tomcat.websocket.executorCoreSize: executor线程池的核心大小。如果不设置,则默认为0。
  • org.apache.tomcat.websocket.executorMaxSize:executor线程池所允许的最大值。如果不设置,则默认为200。
  • org.apache.tomcat.websocket.executorKeepAliveTimeSeconds:executor线程池中空闲进程所保留的最大时间。如果未指定,则默认为60秒。

当使用WebSocket客户端连接到服务器端时,你可以通过javax.websocket.ClientEndpointConfig提供的userProperties来控制建立连接时IO操作的超时时间。该属性为org.apache.tomcat.websocket.IO_TIMEOUT_MS ,它是一个表示超时时间的、单位为毫秒的字符串(String)。默认值为5000(5秒)。

当使用WebSocket客户端连接到安全服务器终端,你可以使用javax.websocket.ClientEndpointConfig提供的userProperties来控制客户端SSL的配置。可以支持下列用户属性:

  • org.apache.tomcat.websocket.SSL_CONTEXT
  • org.apache.tomcat.websocket.SSL_PROTOCOLS
  • org.apache.tomcat.websocket.SSL_TRUSTSTORE
  • org.apache.tomcat.websocket.SSL_TRUSTSTORE_PWD

默认的信任密码为changeit.

如果设置了org.apache.tomcat.websocket.SSL_CONTEXT 属性,那么org.apache.tomcat.websocket.SSL_TRUSTSTOREorg.apache.tomcat.websocket.SSL_TRUSTSTORE_PWD 属性将会被忽略。


-----------------------------------------------------------------------------------

转载自:http://www.softown.cn



================================================================================ Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ================================================================================ Apache Tomcat Version 8.0.15 Release Notes ========= CONTENTS: ========= * Dependency Changes * API Stability * Bundled APIs * Web application reloading and static fields in shared libraries * Security manager URLs * Symlinking static resources * Viewing the Tomcat Change Log * Cryptographic software notice * When all else fails =================== Dependency Changes: =================== Tomcat 8.0 is designed to run on Java SE 7 and later. ============== API Stability: ============== The public interfaces for the following classes are fixed and will not be changed at all during the remaining lifetime of the 8.x series: - All classes in the javax namespace The public interfaces for the following classes may be added to in order to resolve bugs and/or add new features. No existing interface method will be removed or changed although it may be deprecated. - org.apache.catalina.* (excluding sub-packages) Note: As Tomcat 8 matures, the above list will be added to. The list is not c
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值