2012-05-01 2 views
2

로컬 Glassfish 3.1.2 서버 설치에서 websocket을 사용하려고합니다. 내가 사용할 수Glassfish 3.1.2 및 Grizzly가있는 웹 소켓 - 예기치 않은 응답 코드 : 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을 사용하고 있습니다 웹 소켓 지원 이 명령으로 글래스 피시 :

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; 

내가 맥에서 크롬 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.4와 1.9의 1.9 버전을 3.1.2와 함께 사용해야합니다.

+0

grizzly-websockets 1.9.46 jar 및 GlassFish 3.1.2에서 완벽하게 작동합니다. GlassFish 3.1 및 최신 버전의 브라우저를 사용하려면 어떤 버전의 그리즐리 웹 소켓을 사용해야합니까? – 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