Tomcat websocket is not working

Tomcat websocket is not working

本文关键字:working not is websocket Tomcat      更新时间:2023-09-26

我尝试了如下的websocket示例代码,我的浏览器支持HTML5 websocket,但下面的示例代码总是在javascript中提示"关闭"。代码发生了什么变化?

websocket.java

 @WebServlet("/websocket")
    public class websocket extends WebSocketServlet {
        private static final long serialVersionUID = 1L;
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            response.getWriter().println("welcome to websocket 2");
            response.getWriter().flush();   
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }
    @Override
    protected StreamInbound createWebSocketInbound(String arg0,
            HttpServletRequest arg1) {
        return new TheWebSocket();
    }
    private class TheWebSocket extends MessageInbound
    {
        private WsOutbound outbound;
        @Override
        public void onOpen( WsOutbound outbound )
        {
            this.outbound = outbound;
             System.out.println("socket opened!");
        }
        @Override
        public void onTextMessage( CharBuffer buffer ) throws IOException
        {
            try
            {
                    outbound.writeTextMessage( CharBuffer.wrap( "abc testing".toCharArray() ) );
                    System.out.println("Message sent from server.");
            }
            catch ( IOException ioException )
            {
                    System.out.println("error opening websocket");
            }
        }
        @Override
        protected void onBinaryMessage(ByteBuffer arg0) throws IOException {
            // TODO Auto-generated method stub
        }
    }
}

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"  
    pageEncoding="UTF-8"%>  
<!DOCTYPE html>  
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
<title>Index</title>  
<script type="text/javascript">  
var ws = null;  
function startWebSocket() {  
    if ('WebSocket' in window)  
        ws = new WebSocket("ws://localhost:8080/web_test/websocket");  
    else if ('MozWebSocket' in window)  
        ws = new MozWebSocket("ws://localhost:8080/web_test/websocket");  
    else  
        alert("not support");  

    ws.onmessage = function(evt) {  
        alert(evt.data);  
    };  
    ws.onclose = function(evt) {  
        alert("close");  
    };  
    ws.onopen = function(evt) {  
        alert("open");  
    };  
}  
function sendMsg() {  
    ws.send(document.getElementById('writeMsg').value);  
}  
</script>  
</head>  
<body onload="startWebSocket();">  
<input type="text" id="writeMsg"></input>  
<input type="button" value="send" onclick="sendMsg()"></input>  
</body>  
</html>  

当我连接到"http://localhost:8080/web_test/websocket",我得到了正确的消息,那就是"欢迎使用websocket 2"。我的index.jsp文件在web_test之后的根目录中。所以,我的部署应该很好,但有些地方出了问题。我就是搞不清楚。

从servlet代码中注释或删除这两个方法,然后尝试web套接字正常工作。如果servlet中存在这两个,websocket将关闭状态

protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().println("welcome to websocket 2");
        response.getWriter().flush();
    }
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }