页面通过发送消息到服务端
ws.send(JSON.stringify({
room : 'home',
"action" : "join"
}));
服务端执行onOpen方法,但是在session.addMessageHandler(handler);时抛出异常
public void onOpen(Session session, EndpointConfig config) {
changeSessionId(session);
String wsid = session.getId();
WsHandler handler = createHandler(session, config);
handler.setRoomProvider(roomProvider);
handler.setSession(session);
handler.setHttpSession((HttpSession) config.getUserProperties().get("HttpSession"));
handler.setEndpoint(this);
handler.init();
if (!isUndertowSession(session))
try {
session.addMessageHandler(handler);
}
catch (Exception e) {
//log.debug("skip addMessageHandler");
e.printStackTrace();
}
sessions.put(wsid, session);
handlers.put(wsid, handler);
}
java.lang.IllegalStateException: A text message handler has already been configured
at org.apache.tomcat.websocket.WsSession.doAddMessageHandler(WsSession.java:245)
at org.apache.tomcat.websocket.WsSession.addMessageHandler(WsSession.java:206)
at com.wq.module.SocketModule.onOpen(SocketModule.java:28)
at com.wq.module.AbstractWsEndpoint._onOpen(AbstractWsEndpoint.java:95)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.tomcat.websocket.pojo.PojoEndpointBase.doOnOpen(PojoEndpointBase.java:65)
at org.apache.tomcat.websocket.pojo.PojoEndpointServer.onOpen(PojoEndpointServer.java:64)
at org.apache.tomcat.websocket.server.WsHttpUpgradeHandler.init(WsHttpUpgradeHandler.java:133)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:836)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)