我正在尝试使用grails创建聊天应用程序。我使用的是grails 3.0.9和tomcat-8.0.28
抱歉重复
我读了这个question和this ...但那不能解决我的问题..
我已经将javax.websocket-api更改为这样提供:
provided 'javax.websocket:javax.websocket-api:1.0'
但我仍然收到此错误:
Firefox
:Firefox无法在http://103.56.148.50/chat/chatEndPoint/C001D939F2564251C86E646B9127495D"
Chrome
:WebSocket握手期间出错:意外的响应代码:404
你可以看到这个site ...
这是我的代码
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);
}
}
这是在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");
}
这是我在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'
}
=============================================== ====================
我试图运行:grails prod run-app
我的websocket变得不起作用......
但如果我运行grails run-app
,它会起作用是因为ServerEndPointDemo.groovy
我放在/src/main/groovy/com.akiong/ServerEndPointDemo.groovy
?
=============================================== ===============================
删除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