2012-05-01 19 views
2

ローカルGlassfish 3.1.2サーバーインストールでWebSocketを使用しようとしています。私は有効Glassfish 3.1.2とGrizzlyのWebSocket - 予期しない応答コード:405

import org.glassfish.grizzly.Grizzly; 
import java.util.logging.Logger; 
import org.glassfish.grizzly.websockets.WebSocketEngine; 

import javax.servlet.ServletConfig; 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 

public class WebSocketsServlet extends HttpServlet { 

    private static final Logger logger = Grizzly.logger(WebSocketsServlet.class); 
    private final VideoSharingApplication app = new VideoSharingApplication(); 

    @Override 
    public void init(ServletConfig config) throws ServletException { 
     logger.log(Level.SEVERE, "registering"); 
     WebSocketEngine.getEngine().register(config.getServletContext().getContextPath() + "/videosharing", app); 
    } 

    @Override 
    public void destroy() { 
     WebSocketEngine.getEngine().unregister(app); 
    } 
} 

VideoSharingWebSocket.java

import java.util.logging.Logger; 
import org.glassfish.grizzly.websockets.DefaultWebSocket; 
import org.glassfish.grizzly.websockets.ProtocolHandler; 
import org.glassfish.grizzly.websockets.WebSocketListener; 
import org.glassfish.grizzly.Grizzly; 

public class VideoSharingWebSocket extends DefaultWebSocket { 

    private static final Logger logger = Grizzly.logger(VideoSharingWebSocket.class); 

    public VideoSharingWebSocket(ProtocolHandler handler, WebSocketListener... listeners) { 
     super(handler, listeners); 
    } 

} 

VideoSharingApplication.java

import java.util.logging.Level; 
import java.util.logging.Logger; 
import org.glassfish.grizzly.Grizzly; 
import org.glassfish.grizzly.websockets.ProtocolHandler; 
import org.glassfish.grizzly.websockets.WebSocket; 
import org.glassfish.grizzly.websockets.WebSocketApplication; 
import org.glassfish.grizzly.websockets.WebSocketListener; 

import org.glassfish.grizzly.http.HttpRequestPacket; 

public class VideoSharingApplication extends WebSocketApplication { 

    private static final Logger logger = Grizzly.logger(VideoSharingApplication.class); 

    @Override 
    public WebSocket createSocket(ProtocolHandler handler, WebSocketListener... listeners) { 
     logger.log(Level.SEVERE, "createSocket"); 
     return new VideoSharingWebSocket(handler, listeners); 
    } 

    @Override 
    public boolean isApplicationRequest(HttpRequestPacket request) { 
     logger.log(Level.SEVERE, "isApplicationRequest"); 
     return "/videosharing".equals(request.getRequestURI()); 
    } 

    @Override 
    public void onMessage(WebSocket socket, String data) { 
     logger.log(Level.SEVERE, "onMessage"); 
     for (WebSocket webSocket : getWebSockets()) { 
      if (socket != webSocket) { 
       webSocket.send(data); 
      } 
     } 
    } 
} 

<dependency> 
    <groupId>org.glassfish.grizzly</groupId> 
    <artifactId>grizzly-websockets</artifactId> 
    <version>2.2</version> 
</dependency> 

WebSocketsServlet.java:私は私のMavenプロジェクトでグリズリー2.2を使用していますWebsocketのサポートこのコマンドでのGlassFish:

asadmin set configs.config.server-config.network-config.protocols.protocol.http-listener-1.http.websockets-support-enabled=true 

クライアントコードは、app.js:

var network = function() { 
    return { 
     initialize: function() { 
      var url = 'ws://localhost:8080/monApp/videosharing'; 
      var websocket = new WebSocket(url); 
      websocket.name = APP.id; 
      websocket.onopen = function(evt) { 
       alert('onopen'); 
      }; 
      websocket.onerror = function(evt) { 
       alert('onerror'); 
      }; 
      websocket.onmessage = function (evt) { 
       alert('onmessage'); 
       var command = JSON.parse(evt.data); 
       if (command.type == "pause") { 
        APP.pauseVideo(); 
       } else if (command.type == "play") { 
        APP.playVideo(); 
       } else if (command.type == "seeked") { 
        APP.seekVideo(command.currentTime); 
       } else { 
        alert("Unknown command " + command); 
       } 
      }; 
      websocket.onclose = function() 
      { 
       alert('onclose'); 
      }; 
     }, 
     send: function(command) { 
      websocket.send(command); 
     } 
    } 
}; 

var APP = { 
    id: Math.floor(Math.random() * 10000), 

    network: network(), 

    // Cannot use 'this' here after updating window.onload (see below) 
    initialize: function() { 
     APP.network.initialize(); 
     var video = APP.getVideo(); 
     video.addEventListener('play', 
      function (event) { 
       alert('play'); 
       var command = { type: "play" }; 
       APP.network.send(JSON.stringify(command)); 
      }, 
      false); 
     video.addEventListener('pause', 
      function (event) { 
       alert('pause'); 
       var command = { type: "pause" }; 
       APP.network.send(JSON.stringify(command)); 
      }, 
      false); 
     video.addEventListener('seeked', 
      function (event) { 
       alert('seeked'); 
       var command = { type: "seeked", 
           currentTime: APP.getVideo().currentTime }; 
       APP.network.send(JSON.stringify(command)); 
      }, 
      false); 
    }, 

    getVideo: function() { 
     return document.getElementsByTagName("video")[0]; 
    }, 

    pauseVideo: function() { 
     var video = this.getVideo(); 
     video.pause(); 
    }, 

    playVideo: function() { 
     var video = this.getVideo(); 
     video.play(); 
    }, 

    seekVideo: function (currentTime) { 
     var video = this.getVideo(); 
     video.currentTime = currentTime; 
    } 

}; 

window.onload = APP.initialize; 

私は、Macでのクロム18.0.1025.165でこれをテストしています。サーバーのログで

Unexpected response code: 405 

そこにはエラーがなく、唯一の私の「登録する」(WebSocketsServlet)ログが表示されます。ページの読み込みで、私はこのエラーを取得します。

ご意見はありますか?

ありがとうございます。

よろしくお願いいたします。

+0

これをチェックしましたか? http://stackoverflow.com/questions/9964716/grizzly-glassfish-cant-establish-websockets-handshake(特にGlassfishの設定部分) –

答えて

1

GlassFish 3.1.2では、Grizzly 1.9.46が使用されています。 Grizzly 2.xはそのバージョンのGlassFishと互換性がありません。 1.9.49以降の1.9と3.1.2のバージョンを使用する必要があります。

+0

これは、grizzly-websockets 1.9.46 jarとGlassFish 3.1.2で完全に機能します。 GlassFish 3.1と最新バージョンのブラウザで動作させるにはどのバージョンのgrizzly-websocketsを使用しますか? – Yiseli

+0

GlassFish 3.1の使用はお勧めしません。 GlassFish 3.1.2には最新のWSコードがあり、RFC 6455をサポートしています。3.1.2より前のバージョンは日付が付いており、簡単に更新できません。 – rlubke

+0

今、GlassFish 3.1とgrizzly-websockets 2.3-rc3を使用しています。両方ともwebscoketと互換性がありますか? – kamlesh0606

関連する問題