NutzCN Logo
问答 nutwebsocket发消息时报错
发布于 2788天前 作者 蛋蛋的忧伤 2783 次浏览 复制 上一个帖子 下一个帖子
标签:

我在做离线消息,当socket连接时走onOpen方法时,我查数据库是否有我的离线消息,如果有我以此构建消息实体,然后发送给当前session,代码如下

/**
	 * 连接打开时的回调
	 * @param session
	 */
	public void onOpen(Session session, EndpointConfig config) {
		changeSessionId(session);
        String wsid = session.getId();
     	String strs = session.getQueryString();//获取请求参数
		System.out.println(strs);
		String username = strs.split("=")[1];
		username = username.substring(0,username.length()-3);//截取用户名
		int id = Integer.valueOf(strs.split("=")[2]);	//截取id
		try {
			username = URLDecoder.decode(username,"utf-8");	//转码
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		this.username = username;
		this.id = id;
		this.session = session;
		//维护当前session
        WsHandler handler = createHandler(session, config);
//        handler.setRoomProvider(roomProvider);
//        handler.setSession(session);
        session.addMessageHandler(handler);
        sessions.put(wsid, session);
        handlers.put(username, handler);
        
        try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
        
        //查看是否有离线消息
        List<ChatMessage> list = chatMessageDao.getUnRead(id,username);
        if(list!=null && list.size()>0){
        	for(ChatMessage c : list){
        		if(1 == c.getType()){	//单聊
        			SendMessage sendMessage = new SendMessage();
            		sendMessage.setUsername(c.getUsername());
            		sendMessage.setAvatar(c.getAvatar());
            		sendMessage.setId(c.getFrom().toString());
            		sendMessage.setType("friend");
            		sendMessage.setContent(c.getContent());
//            		if(to.get("username").equals(username)){
//            			sendMessage.setMine(true);
//            		}else{
//            			sendMessage.setMine(false);
//            		}
            		sendMessage.setFromid(c.getFrom().toString());
            		sendMessage.setTimestamp(Long.valueOf(c.getTimestamp()));
            		String strJson =Json.toJson(sendMessage);
            		System.out.println("好友消息:"+strJson);
            		session.getAsyncRemote().sendText(strJson);
        		}else if(2 == c.getType()){	//群聊
        			System.out.println("send group message...");
            		
            		//构建转发消息实体
            		SendMessage sendMessage = new SendMessage();
            		sendMessage.setUsername(c.getUsername());
            		sendMessage.setAvatar(c.getAvatar());
            		sendMessage.setId(c.getFrom().toString());
            		sendMessage.setType("group");
            		sendMessage.setContent(c.getContent());
            		sendMessage.setFromid(null);
//            		if(to.get("username").equals(username)){
//            			sendMessage.setMine(true);
//            		}else{
//            			sendMessage.setMine(false);
//            		}
//            		sendMessage.setFromid(mine.get("id"));
            		sendMessage.setTimestamp(Long.valueOf(c.getTimestamp()));
            		String strJson =Json.toJson(sendMessage);
//            		String strJson = sendMessage.toJson();
            		System.out.println(strJson);
//            		session.getAsyncRemote().sendText(strJson);
            		try {
						session.getBasicRemote().sendText(strJson);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
        		}
        	}
        	
//        	session.getAsyncRemote().sendText(arg0);
        }
        System.out.println(username+"上线了...");
		System.out.println("当前在线:"+handlers);
	}
	

但是抛了这个异常:

五月 05, 2017 9:06:40 上午 org.apache.coyote.AbstractProtocol$AbstractConnectionHandler process
严重: Error reading request, ignored
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:1177)
	at org.apache.tomcat.websocket.WsRemoteEndpointImplBase$StateMachine.textStart(WsRemoteEndpointImplBase.java:1140)
	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 com.mychat.socket.NutSocket.onOpen(NutSocket.java:97)
	at org.apache.tomcat.websocket.server.WsHttpUpgradeHandler.init(WsHttpUpgradeHandler.java:127)
	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:703)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Unknown Source)

有时候是好的,但有时候就抛这个异常,百度的呢是说onMessage什么的返回值什么鬼的,但是好像跟那么没啥关系呀

7 回复

js端发hi的时候传离线消息,而不是onOpen的时候

我现在是将每条消息都存数据库了,在存的时候判断对方是否在线,如果离线就标记一下,当对方上线后再查他的离线消息,不应该在onOpen时做这个操作么?

js发hi过来的时候查

在onMessage的时候才处理

你的意思是我在js的onOpen回调里手动发一条消息 "hi" ?

感谢纠正思路,不然我这坑越走越深...

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