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

        }
    }

}

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 の後のルート ディレクトリにあります。したがって、私の展開は問題ないはずですが、どこかが間違っています。私はそれを理解することはできません。

4

1 に答える 1

6

これら 2 つのメソッドをコメント化するか、サーブレット コードから削除してから、正常に機能する Web ソケットを試してください。これら 2 つがサーブレットに存在する場合、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 に答える