与'ws:// localhost:8080 / CollabEdit / echo'的WebSocket连接失败:WebSocket握手期间出错:意外响应代码:404

时间:2015-04-08 18:03:47

标签: java java-ee servlets websocket glassfish-4

我正在尝试在Servlets项目中使用websockets。 J2EE版本:3.1 服务器:Glassfish 4.0 但是我收到的错误是:

WebSocket connection to 'ws://localhost:8080/CollabEdit/echo' failed: 
Error during WebSocket handshake: Unexpected response code: 404

另外,当我在服务器上部署它时,我得到 java.lang.Instantiation 异常。

当项目中只有2个文件时,我用作WebSockets客户端和服务器的文件运行良好。 自从我将它集成到我的servlet项目中后,它停止了工作。所以,我怀疑它是因为我们的工作流程而在某处:

工作流程如下:

  1. index.html - >这是用户登录的位置

  2. 调用LoginServlet调用html create_file.html

  3. 一个html表单,要求用户提供要创建的文件名并为其选择一个扩展名。 在数据库中插入此记录时,会生成相应的GUID。

  4. 此数据发送到名为Collaborate.java的servlet .Collaborate.java检索此GUID并使用此GUID前置的字符串文档“动态”创建URL。

  5. 使用此GUID动态生成URL后,控件将转发到名为Document.java的servlet(带有映射Document / *),该servlet仅将控件转发给main.js.

  6. 此js文件是客户端(具有类似http://applicationname/Document/GUID的网址),按如下方式调用Web套接字服务器:

    var websocket = new websocket(“ws:// localhost:8080 / applicationname / echo”);

  7. websocket服务器有这个注释:

    @ServerEndpoint(“/ echo”)

  8. 此流程中可能出现此错误的确切错误是什么?

    编辑:当我在服务器上部署此应用程序时,我得到一个例外情况:

     java.lang.InstantiationException
        at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:135)
        at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:5297)
        at org.apache.catalina.core.StandardContext.start(StandardContext.java:5909)
        at com.sun.enterprise.web.WebModule.start(WebModule.java:691)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:1041)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:1024)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:747)
        at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:2278)
        at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:1924)
        at com.sun.enterprise.web.WebApplication.start(WebApplication.java:139)
        at org.glassfish.internal.data.EngineRef.start(EngineRef.java:122)
        at org.glassfish.internal.data.ModuleInfo.start(ModuleInfo.java:291)
        at org.glassfish.internal.data.ApplicationInfo.start(ApplicationInfo.java:352)
        at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:497)
        at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:219)
        at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:491)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:527)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:523)
        at java.security.AccessController.doPrivileged(Native Method)
        at javax.security.auth.Subject.doAs(Subject.java:356)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl$2.execute(CommandRunnerImpl.java:522)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:546)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1423)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1500(CommandRunnerImpl.java:108)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1762)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1674)
        at com.sun.enterprise.v3.admin.AdminAdapter.doCommand(AdminAdapter.java:534)
        at com.sun.enterprise.v3.admin.AdminAdapter.onMissingResource(AdminAdapter.java:224)
        at org.glassfish.grizzly.http.server.StaticHttpHandler.service(StaticHttpHandler.java:297)
        at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:246)
        at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
        at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
        at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
        at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
        at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
        at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
        at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
        at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
        at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
        at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
        at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
        at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
        at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
        at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
        at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
        at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
        at java.lang.Thread.run(Thread.java:744)
    Caused by: javax.servlet.ServletException: Web socket server initialization failed.
        at org.glassfish.tyrus.servlet.TyrusServletFilter.init(TyrusServletFilter.java:135)
        at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:275)
        at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:131)
        ... 46 more
    Caused by: javax.websocket.DeploymentException: Decoder for type: class com.CollabEdit.Message has not been registered.
    Decoder for type: class com.CollabEdit.Message has not been registered.
    Found Equivalent paths. Added path: '/CollabEdit/echo' is equivalent with '/CollabEdit/echo'.
        at org.glassfish.tyrus.core.ErrorCollector.composeComprehensiveException(ErrorCollector.java:83)
        at org.glassfish.tyrus.server.TyrusServerContainer.start(TyrusServerContainer.java:144)
        at org.glassfish.t
    

    main.js看起来像这样:

    var webSocket;
    var messages = [];
    var index=-1;
    //var link = "ws" + document.URL.substring(4, document.URL.length) ;
    var link = "ws://localhost:8080/CollabEdit/Document/*";
    var link3 = "ws://localhost:8080/CollabEdit/echo";
    var firstMessage;
    var to=[];
    var path = document.URL;
    var array = path.split("/");
    var temp = array[array.length - 1];
    var link2 = temp.substring(temp.length - 36, temp.length);
    
    require([ 'dojo/on' ], function(on) {
        cm.on('change', function(arg1, arg2) {
            actionToPerform = arg2.origin;
            console.log("typde dekh: ", typeof actionToPerform);
            lineLocation = arg2.from.line;
            charLocation = arg2.from.ch;
            data = arg2.text;
            console.log("Action: ", actionToPerform, " Data: ", data, " line: ",
                    lineLocation, " charLocation: ", charLocation);
            console.log("Location: " + window.location.href);
            var cookie = document.cookie;
            console.log("cookie dekh: ");
            console.log(cookie);
            $.ajax({
                method : "POST",
                url : "../OTServer",
                data : {
    
                    "action" : "'" + actionToPerform + "'",
                    "data" : "'" + data + "'",
                    "lineLocation" : "'" + lineLocation + "'",
                    "charLocation" : "'" + charLocation + "'"
                },
                success: function(data){
                    // yup run kr isse :P
    
                    console.log("in success, got this: ",data);
                },
                error: function(data){
    
               }
            }).done(function(msg) {
                //alert("Response: " + msg);
            });
        });
    });
    
    
    $.ajax({
        method : "POST",
        async : false,
        url : "/CollabEdit/GetInfo",
        success : function(data) {
            console.log("data sent successfully: ",data);
        },
        error : function(data) {
            console.log("Unsuccessful transmission " + data);
        }}).done(
            function(data) {
                firstMessage = {
                    "from":data['email']
                }
        });
    
    
    //this will get the users with whom this file is being shared
    $.ajax({
        method : "POST",
        async : false,
        url : "/CollabEdit/DisplaySEmails",
        data: {"url": link2},
        success : function(data) {
            console.log("data sent successfully: ",data);
        },
        error : function(data) {
            console.log("Unsuccessful transmission " + data);
        }}).done(
            function(data) {
                for(var i in data)
                   to[i] = data[i];
        });
    
    
    //calling the openSocket
    openSocket(link3);
    
    //console.log("chal gya m chajj naal");
    //alert("yaaaaay");
    function openSocket(link3){
        // Ensures only one connection is open at a time
        if(webSocket !== undefined && webSocket.readyState !== WebSocket.CLOSED){
            //writeResponse("WebSocket is already opened.");
            return;
        }
        // Create a new instance of the websocket
        console.log("LINK ISSSSSSSSSSS" + link);
    
        webSocket = new WebSocket(link3);
    
        /**
         * Binds functions to the listeners for the websocket.
         */
         //calls onOpen of server when connection is established
        webSocket.onopen = function(event){
            if(event.data === undefined)
                return;
            //alert("onOpen executed");
            writeResponse(event.data);
        };
    
      //Calls onMessage ofServer when a message is sent
        webSocket.onmessage = function(event){
            writeResponse(event.data);
        };
    
       ///calls onClose when connection is closed
        webSocket.onclose = function(event){
            messages.innerHTML += "<br/>" + "Connection closed";
        };
    }
    
    /**
     * Sends the value of the text input to the server
     */
    
    //Data is to SEND hona h 
    function sendText(data){
        var json = JSON.stringify(data);
        webSocket.send(json);
    }
    
    function closeSocket(){
        webSocket.close();
    }
    
    ///jado response aaega MEANS jab data aaega
    function writeResponse(json){
    
        var response = JSON.parse(json);
        var output = response.data;
    

    P.S:我很确定代码中没有错误。它看起来只是一个控制流问题。 任何帮助将深表感谢。如果我错过任何东西,请考虑询问细节。

0 个答案:

没有答案