要配置Tomcat以支持WebSocket协议,需要做以下几个步骤:
<Connector port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol"
connectionTimeout="20000"
redirectPort="8443" />
<servlet>
<servlet-name>WebSocketServlet</servlet-name>
<servlet-class>com.example.WebSocketServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>WebSocketServlet</servlet-name>
<url-pattern>/websocket</url-pattern>
</servlet-mapping>
var webSocket = new WebSocket("ws://localhost:8080/yourAppName/websocket");
webSocket.onmessage = function(event) {
console.log("Received message: " + event.data);
};
这样就完成了Tomcat的WebSocket配置,可以通过WebSocket协议与Tomcat服务器进行双向通信。