NutzCN Logo
问答 websocket集成时报错
发布于 2593天前 作者 小小小郑 2558 次浏览 复制 上一个帖子 下一个帖子
标签:

报如下tomcat错误,是因为线程没同步吗?我全部用的异步方法给客户端发送消息,wsserver没有使用同步的方法

java.lang.IllegalStateException: The remote endpoint was in state [TEXT_FULL_WRITING] which is an invalid state for called method
	at org.apache.tomcat.websocket.WsRemoteEndpointImplBase$StateMachine.checkState(WsRemoteEndpointImplBase.java:1206)
	at org.apache.tomcat.websocket.WsRemoteEndpointImplBase$StateMachine.textStart(WsRemoteEndpointImplBase.java:1169)
	at org.apache.tomcat.websocket.WsRemoteEndpointImplBase.sendStringByCompletion(WsRemoteEndpointImplBase.java:213)
	at org.apache.tomcat.websocket.WsRemoteEndpointImplBase.sendStringByFuture(WsRemoteEndpointImplBase.java:201)
	at org.apache.tomcat.websocket.WsRemoteEndpointAsync.sendText(WsRemoteEndpointAsync.java:53)
	at org.nutz.plugins.mvc.websocket.AbstractWsEndpoint.sendJson(AbstractWsEndpoint.java:215)
	at com.game.brag.server.MyHandler.onMessage(MyHandler.java:95)
	at com.game.brag.server.NutzWsServer.onClose(NutzWsServer.java:59)
	at org.apache.tomcat.websocket.WsSession.fireEndpointOnClose(WsSession.java:515)
	at org.apache.tomcat.websocket.WsSession.onClose(WsSession.java:496)
	at org.apache.tomcat.websocket.WsFrameBase.processDataControl(WsFrameBase.java:348)
	at org.apache.tomcat.websocket.WsFrameBase.processData(WsFrameBase.java:290)
	at org.apache.tomcat.websocket.WsFrameBase.processInputBuffer(WsFrameBase.java:131)
	at org.apache.tomcat.websocket.server.WsFrameServer.onDataAvailable(WsFrameServer.java:67)
	at org.apache.tomcat.websocket.server.WsHttpUpgradeHandler$WsReadListener.onDataAvailable(WsHttpUpgradeHandler.java:204)
	at org.apache.coyote.http11.upgrade.AbstractServletInputStream.onDataAvailable(AbstractServletInputStream.java:203)
	at org.apache.coyote.http11.upgrade.AbstractProcessor.upgradeDispatch(AbstractProcessor.java:93)
	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:635)
	at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2549)
	at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2538)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:744)
7 回复

onClose的时候不能发东西了

嗯 因为需要在用户离开时让吧该用户移除房间,我确实在onclose的时候直接向handler发送离开房间消息,请问还有别的方式能实现这个功能吗,我也觉得这样写有些问题

	@Override
    public void onClose(Session session, CloseReason closeReason) { 
		synchronized(session){
			String message = "{action:\"left\",room:\""+room+"\",openId:\""+openId+"\"}";
			handler.onMessage(message);
		}
		//人数-1
        subOnlineCount();  
        System.out.println("有一链接关闭!当前在线人数为" + getOnlineCount()); 
        //调用父类的onclose
  		super.onClose(session, closeReason);
    } 

说错了不是移出房间,是通知其他用户该用户已经离开。要怎么做到?

把session id也传给handler, 在发送给房间的时候,跳过该id

1、客户端每10S主动ping一次服务器
2、服务器发送当前还在线的有所用户
3、客户端接受新消息,判断目前的所有用户是否都存在于新消息中,如果不存在则移出该房间。
请问大神,我这样设计会不会有什么问题,每次10秒ping一次会不会对服务器负担太大?

还是说设计成每次客户端发消息来都给他返回所有在线的用户数据

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