grails 3.0.9 WebSocket 404 error

[亡魂溺海] 提交于 2019-12-23 05:05:06

问题


i am trying to create chat application with grails. i am using grails 3.0.9 and tomcat-8.0.28

Sorry for duplicate

i read this question and this... but thats not solve my problem..

i already change javax.websocket-api to provided like this :

provided 'javax.websocket:javax.websocket-api:1.0'

but i still get this error :

Firefox: Firefox can't establish a connection to the server at http://103.56.148.50/chat/chatEndPoint/C001D939F2564251C86E646B9127495D"

Chrome: Error during WebSocket handshake: Unexpected response code: 404

you can see this site ...

this is my code

package com.akiong

import grails.util.Environment
import grails.util.Holders

import javax.servlet.ServletContext
import javax.servlet.ServletContextEvent
import javax.servlet.ServletContextListener
import javax.servlet.annotation.WebListener
import javax.websocket.EndpointConfig
import javax.websocket.OnClose
import javax.websocket.OnError
import javax.websocket.OnMessage
import javax.websocket.OnOpen
import javax.websocket.Session
import javax.websocket.server.PathParam
import javax.websocket.server.ServerContainer
import javax.websocket.server.ServerEndpoint
import com.akiong.services.ChatService
import javax.websocket.EncodeException
//import org.codehaus.groovy.grails.commons.ApplicationHolder as AH

import javax.servlet.annotation.WebListener
import java.util.concurrent.CopyOnWriteArraySet

@WebListener
@ServerEndpoint(value="/chat/chatEndPoint/{username}")

public class ServerEndPointDemo implements ServletContextListener {
    private static HashMap<String, String> usersMap = new HashMap<String, String>();
    private static final Set<ServerEndPointDemo> connections = new CopyOnWriteArraySet<>();
    private String username
    private Session session

    @OnOpen
    public void  handleOpen(Session session,@PathParam("username") String user){
        System.out.println("-------------------------------------");
        System.out.println(session.getId() + " has opened a connection");
        println "user = "+user
        connections.add(this);
        this.username   = user
        this.session    = session
    }

    @OnClose
    public void  handleClose(Session session){
        System.out.println("Session " +session.getId()+" has ended");
    }

    @OnMessage
    public String handleMessage(String message,Session session){
       println "message = "+message
       println "session= "+session.getId()

    }

    @OnError
    public void  handleError(Throwable t){
        connections.remove(this);
        println "username = "+username
        removeUserInMap(session.getId(), username);
    }
}

this is in javascript

var wsUri = "ws://103.56.148.50/chat/chatEndPoint/"+room;
    var webSocket = new WebSocket(wsUri);



    webSocket.onopen    = function(message) {OnOpen(message);};
    webSocket.onclose   = function(message) {OnClose(message);};
    webSocket.onerror   = function(message) {OnError(message);};
    webSocket.onmessage = function(message) {OnMessage(message);};


function OnOpen(message){
    console.log("Open Socket");
}

function OnClose(message){
   console.log("Close = "+message);
    webSocket.close();
}

function OnError(message){
    console.log("ERROR = "+message);
}

function OnMessage(message){
  webSocket.send("success");
}

this is my dependencies in build.gradle

dependencies {
    compile "org.springframework.boot:spring-boot-starter-logging"
    compile "org.springframework.boot:spring-boot-starter-actuator"
    compile "org.springframework.boot:spring-boot-autoconfigure"
    provided "org.springframework.boot:spring-boot-starter-tomcat"
    compile "org.grails:grails-dependencies"
    compile "org.grails:grails-web-boot"

    compile "org.grails.plugins:hibernate"
    compile "org.grails.plugins:cache"
    compile "org.hibernate:hibernate-ehcache"
    compile "org.grails.plugins:scaffolding"

    runtime "org.grails.plugins:asset-pipeline"

    testCompile "org.grails:grails-plugin-testing"
    testCompile "org.grails.plugins:geb"

    console "org.grails:grails-console"

    //-----------------------------------here i add my code-------------------
    compile 'org.grails.plugins:spring-security-core:3.0.0.M1'

    runtime 'mysql:mysql-connector-java:5.1.20'

    compile "org.grails.plugins:mail:2.0.0.RC2"

    compile 'org.grails.plugins:jms:2.0.0.M1' //http://gpc.github.io/jms/snapshot/guide/introduction.html

    runtime 'org.apache.activemq:activemq-spring:5.11.1'

    provided 'javax.websocket:javax.websocket-api:1.0'

}

===================================================================

i tried to run : grails prod run-app

my websocket become not work...

but it work if i run grails run-app

is that because ServerEndPointDemo.groovy i put at /src/main/groovy/com.akiong/ServerEndPointDemo.groovy ?

==============================================================================

i get this error after remove if (Environment.current == Environment.DEVELOPMENT)

Configuring Spring Security Core ...
... finished configuring Spring Security Core

anc
Environment.current = PRODUCTION
Environment.DEVELOPMENT = DEVELOPMENT
Environment.current = PRODUCTION
Environment.DEVELOPMENT = DEVELOPMENT
26-Nov-2015 23:29:40.407 SEVERE [localhost-startStop-1] org.apache.catalina.core.StandardContext.startInternal One or more listeners failed to start. Full details will be found in the appropriate container log file
26-Nov-2015 23:29:40.420 SEVERE [localhost-startStop-1] org.apache.catalina.core.StandardContext.startInternal Context [] startup failed due to previous errors
26-Nov-2015 23:29:40.769 SEVERE [localhost-startStop-1] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [ROOT] created a ThreadLocal with key of type [org.grails.web.converters.configuration.ConvertersConfigurationHolder$2] (value [org.grails.web.converters.configuration.ConvertersConfigurationHolder$2@5ce7a180]) and a value of type [java.util.HashMap] (value [{}]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
26-Nov-2015 23:29:40.794 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive /root/apache-tomcat-8.0.28/webapps/ROOT.war has finished in 94,084 ms
26-Nov-2015 23:29:40.796 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-80"]
26-Nov-2015 23:29:40.802 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-nio-8009"]
26-Nov-2015 23:29:40.803 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 94281 ms

来源:https://stackoverflow.com/questions/33914793/grails-3-0-9-websocket-404-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!