我在32位Windows XP和64位Debian 6.0.5 Linux上安装了Apache Tomcat 7.0.29,并尝试使用websocket示例。但是它们运行不正常。我什至无法连接到服务器。
使用echo示例(选择消息API)并按Connect(连接)按钮,将不会发生任何事情。但是20秒后,消息“ WebSocket连接已关闭”出现在日志文本区域中。但是正如其他文章所述,这是一个已知问题。
当使用自制的Websocket应用程序并尝试连接到服务器时,我注意到MessageInbound#onOpen方法的日志语句已打印,因此该方法被调用。但是,浏览器Javascript部分中的onopen回调未触发。但是在终止tomcat实例后,立即调用客户端onopen,紧接着是onclose。
有没有人可以确认这种行为或类似的行为?还是有人在Tomcat 7上使用过websocket示例?谢谢你的帮助。
更新: 我为自己创建的示例应用程序添加了代码。
这是服务器部分WebSocketTestServlet.java:
import java.io.IOException; import java.nio.ByteBuffer; import java.nio.CharBuffer; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServletRequest; import org.apache.catalina.websocket.MessageInbound; import org.apache.catalina.websocket.StreamInbound; import org.apache.catalina.websocket.WebSocketServlet; import org.apache.catalina.websocket.WsOutbound; @WebServlet(value = "/websocket", loadOnStartup = 1) public class WebSocketTestServlet extends WebSocketServlet { private static final long serialVersionUID = 1L; @Override protected StreamInbound createWebSocketInbound(String subProtocol, HttpServletRequest request) { return new WebSocketTestInbound(); } public static final class WebSocketTestInbound extends MessageInbound { @Override protected void onOpen(WsOutbound outbound) { System.out.println("WebSocketTestServlet#onOpen"); } @Override protected void onClose(int status) { System.out.println("WebSocketTestServlet#onClose"); } @Override protected void onBinaryMessage(ByteBuffer message) throws IOException { System.out.println("WebSocketTestServlet#onBinaryMessage received: " + message); } @Override protected void onTextMessage(CharBuffer message) throws IOException { System.out.println("WebSocketTestServlet#onTextMessage received: " + message); } } }
这里是唯一的JSF facelet main.xhtml:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html"> <h:head> <title>Test suite</title> <script type="text/javascript"> var ws = null; function connect() { var wsUrl = $('#id_wsUrl'); var wsMsg = $('#id_wsMsg'); var wsSnd = $('#id_wsSnd'); var wsCon = $('#id_wsCon'); var url = wsUrl.val(); // Connect if (ws == null) { if (window.WebSocket) ws = new WebSocket(url); else if (window.MozWebSocket) ws = new MozWebSocket(url); else { alert('WebSocket not supported by this browser'); return; } ws.onopen = function() { alert('Connection opened!'); wsMsg.removeAttr('disabled'); wsSnd.removeAttr('disabled'); wsUrl.attr('disabled', 'disabled'); wsCon.val('Disconnect'); }; ws.onclose = function() { alert('Connection closed!'); wsMsg.attr('disabled', 'disabled'); wsSnd.attr('disabled', 'disabled'); wsUrl.removeAttr('disabled'); wsCon.val('Connect'); }; ws.onmessage = function(event) { console.log('Inbound message: ' + event.data); alert('Inbound message: ' + event.data); }; ws.onerror = function() { alert('Connection error!!!'); }; } // Disconnect else { ws.close(); ws = null; } } function sendMessage() { if (ws) { var wsMsg = $('#id_wsMsg'); var data = wsMsg.val(); wsMsg.val(''); if (data.length > 0) ws.send(data); } } </script> </h:head> <h:outputScript target="head" library="js" name="jquery-1.8.0.js" /> <h:body> <h1>WebSocket tests</h1> <h:panelGrid columns="2"> <h:outputLabel for="id_wsUrl" value="WebSocket server URL:" /> <h:panelGroup> <h:inputText id="id_wsUrl" style="width: 250px;" value="ws://localhost:8080/Testapp/websocket" /> <h:commandButton type="button" id="id_wsCon" value="Connect" onclick="connect();" /> </h:panelGroup> <h:outputLabel for="id_wsMsg" value="WebSocket outbound message" /> <h:panelGroup> <h:inputText id="id_wsMsg" style="width: 250px;" value="" disabled="true" /> <h:commandButton type="button" id="id_wsSnd" value="Send" disabled="true" onclick="sendMessage();" /> </h:panelGroup> </h:panelGrid> </h:body> </html>
我不知道该怎么做,但我希望它对编写代码有帮助。
塞巴斯蒂安
扩展WebSocketServlet现在已经废弃,甚至在Tomcat中7.由于Tomcat的 7.0.47 标准javax.websocket- api提供,这意味着你可以轻松地通过使用创建的WebSocket端点@ServerEndpoint上你的类注释。确保按提供的方式设置依赖项。
WebSocketServlet
javax.websocket- api
@ServerEndpoint
尝试直接打到Tomcat的起点,因为它作为代理在Apache或IIS后面运行也会引起问题。