0

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
Community
  • 1
  • 1
  • I think it should just work under your current grails... just do a grails run app in the app folder ( I haven't tried it). Also https://github.com/vahidhedayati/grails-wschat-plugin/blob/master/src/main/groovy/grails/plugin/wschat/WsChatEndpoint.groovy#L35 serverContainer.addEndpoint(WsChatEndpoint) this was a hack for grails 2 under development mode. Maybe you can try adding that to your current end point and wrapping no environment around it to see if it makes a difference. I will have to try out the test project at home it won't be till tomorrow to see if its working. – V H Nov 25 '15 at 13:58
  • i tried to run with grails 3.0.9...but i get this error.. | Error Failed to compile stop-app.groovy: startup failed: stop-app.groovy: 60: unable to resolve class GroovyMBean @ line 60, column 18. def mbean = new GroovyMBean(server, objectName) ^ –  Nov 25 '15 at 14:00
  • then i download graisl 3.0.1 to run it..but still get that error –  Nov 25 '15 at 14:00
  • @vahid hi...i get this answer from grails.slack.com `gryphonest`: this might not be your problem, but we had some issues deploying a websocket-enabled app (Grails 3.0.9, Tomcat 7) simply because the websocket connection was configured to connect at the root context. Which worked under grails run-app, because the app was running as localhost:[port]/, but failed on Tomcat because the app was deployed as server:[port]/[​*appname*​]/(edited) ​[9:11] We just changed our tomcat config to deploy the WAR file to the root context and it works fine now. –  Nov 25 '15 at 14:25
  • Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackoverflow.com/rooms/96398/discussion-on-question-by-akiong-grails-3-0-9-websocket-404-error). – elixenide Nov 28 '15 at 03:40

0 Answers0