NutzCN Logo
问答 关于websocket运行问题
发布于 188天前 作者 wx_8868jgbos3h956ejd54e 1168 次浏览 复制 上一个帖子 下一个帖子
标签:

页面通过发送消息到服务端

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)
2 回复

但没有影响,tomcat/jetty/undertow的行为各有不同

好的,非常感谢👍

添加回复
请先登陆
回到顶部