6

我尝试了如下 websocket 示例代码,我的浏览器支持 HTML 5 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

        }
    }

}

索引.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 之后的根目录下。所以,我的部署应该没问题,但有些地方是错误的。我就是想不通。

4

1 回答 1

6

从您的 servlet 代码中注释或删除这两个方法,然后尝试 web sockets 工作正常。如果这两个存在于 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
    }
于 2012-09-12T10:22:23.007 回答