Websocket停止在tomcat 7.0.50上运行

时间:2014-03-01 08:22:30

标签: websocket tomcat7 apache2.4

我最近在tomcat 7.0.50上实现了websocket。我的tomcat在apache 2.4后面运行。 我使用Strtus 2和Spring Security。它已停止工作。

问题:  Websocket连接已成功打开。  Javascript事件处理程序成功触发。  Apache日志显示请求被接受为具有101代码的协议升级请求  Tomcat访问日志还显示用于协议升级的相同代码101

但是在服务器端,不会触发onOpen消息。  我在javascript的事件处理程序中打开连接时尝试发送消息。  javascript中没有错误,但是从不调用服务器端endPoint的onmessage,就像从未调用onOpen一样。

以下是我在停止工作后所做的更改:

 Windows Host file : I use dummy host name through Windows host file:
127.0.0.1    phasingapp.com
127.0.0.1    pricer.com

wesocket的网址:

old  :  ws://localhost:86/websocket/filuploadtrackerendpoint
new : ws://phasingapp.com:86/websocket/filuploadtrackerendpoint

为apache添加了虚拟主机配置:

 <Location /jkweb/websocket/>
    Require host phasingapp.com granted
</Location>    

JkMount /jkweb/* localtomcat
JkUnMount /jkweb/project/content/*  localtomcat
JkUnMount /jkweb/angle/content/*  localtomcat
JkUnMount /jkweb/index.jsp localtomcat
JkUnMount /jkweb/ localtomcat

ProxyRequests Off
ProxyPreserveHost On

ProxyPass /jkweb  !
ProxyPass /jkweb/  !
ProxyPass /jkweb/websocket/  ws://localhost:8080/jkweb/websocket/
ProxyPassReverse /jkweb/websocket/  ws://localhost:8080/jkweb/websocket/
ProxyPass /  http://localhost:8080/jkweb/
ProxyPassReverse /  http://localhost:8080/jkweb/

Struts 2:

 changed url pattern for Struts 2 filter from ".action to " /* ". Also added exclusion   filters
 to strtus2  for webscoket urls :

<constant name="struts.action.excludePattern" value=".*websocket/.*"/>

似乎一切正常,因为我在apache,tomcat,spring security和Struts2日志中没有任何错误。

但是在服务器端没有调用处理程序?

之前我遇到过类似的问题,但是因为我在我的WEB-INF / lib中放了websocket-api jar,这阻止了websocket连接的打开。现在连接已打开,但在此之后没有调用处理程序,并且在一段时间后连接被关闭显然是因为我认为是暂时的。

这是我的ServerEndPoint类:

      package com.jkweb.websocket;

        import java.io.IOException;
        import java.util.HashMap;
        import java.util.List;
        import java.util.Map;

        import javax.websocket.EndpointConfig;
        import javax.websocket.OnMessage;
        import javax.websocket.OnOpen;
        import javax.websocket.Session;
        import javax.websocket.server.PathParam;
        import javax.websocket.server.ServerEndpoint;

        import org.slf4j.Logger;
        import org.slf4j.LoggerFactory;

        @ServerEndpoint(value="/websocket/fileuploadtracker")
       public class FileUploadTrackerEndPoint{
       private static final Logger logger = LoggerFactory
            .getLogger(FileUploadTrackerEndPoint.class);
       private static HashMap<String,Session> socketConnectionMap = new    HashMap<String,Session>();

    @OnOpen 
    public void open(Session session) {
        Map<String,List<String>> paramMap = session.getRequestParameterMap();
        List<String> uniqueTockenValues = paramMap.get("uniqueTocken");
        if(uniqueTockenValues != null && uniqueTockenValues.size() > 0){
            socketConnectionMap.put(uniqueTockenValues.get(0),session); 
        }
    }
    @OnMessage
    public void onMessage(Session session, String msg) {
        try {
            session.getBasicRemote().sendText(msg);
        } catch (IOException e) {
            logger.error(e.getMessage());
        }
    }

    public static void sendMessage(String uniqueTocken,String msg){
        try {
            Session wsSession = socketConnectionMap.get(uniqueTocken);
            wsSession.getBasicRemote().sendText(msg);
        } catch (IOException e) {
            logger.error(e.getMessage());
        }
    }
}

和我的javascript代码:

createFileUploadWebSocket : function(){
            var that = this;
            var uniqueTocken = $("#uniqueTocken").val(),
            wsurl =  "ws://phasingapp.com:86/jkweb/websocket/fileuploadtracker?uniqueTocken="+uniqueTocken,
            ws;
            ws = new WebSocket(wsurl);
             ws.onopen = function()
             {
                 alert("opened..") //I get this aler sothis is called
                 ws.send("test");//however this has not effect ...no error but server end point is not called
             };
             ws.onmessage = function (evt) 
             { 
                 message = evt.data;
                 if(message == "Done."){
                     alert("closing..");
                     ws.close();
                     $('#progressbar').progressbar('option','value',100);
             }
                 var currentVal = $('#progressbar').progressbar('option','value');
                 $('#progressbar').progressbar('option','value',currentVal <= 80 ?currentVal+1 : currentVal);
             };
             ws.onclose = function(evt)
             { 
                // websocket is closed.
                alert("Connection is closed..."+evt.code + ":"+evt.reason ); 
             };

             ws.onerror = function(evt){
                 alert("Connection is closed..."+evt.code + ":"+evt.reason );
             };

        },

1 个答案:

答案 0 :(得分:1)

再次提到我的所有其他websocket问题,我在配置(错误配置)中找到了答案。

 ProxyPass /jkweb  !
 ProxyPass /jkweb/  !
 ProxyPass /jkweb/websocket/  ws://localhost:8080/jkweb/websocket/
 ProxyPassReverse /jkweb/websocket/  ws://localhost:8080/jkweb/websocket/
 ProxyPass /  http://localhost:8080/jkweb/
 ProxyPassReverse /  http://localhost:8080/jkweb/

我改变了proxypass指令的顺序,如下所示:

 ProxyPass /jkweb/websocket/  ws://localhost:8080/jkweb/websocket/
 ProxyPassReverse /jkweb/websocket/  ws://localhost:8080/jkweb/websocket/
 ProxyPass /jkweb  !
 ProxyPass /jkweb/  !
 ProxyPass /  http://localhost:8080/jkweb/
 ProxyPassReverse /  http://localhost:8080/jkweb/

将它放在websocket的proxypass指令之前导致每个以jkweb /开头的请求导致它绕过代理。