java吧 关注:1,286,251贴子:12,811,129
  • 22回复贴,共1

webScoket求助!!!!!!!!!!!!1

只看楼主收藏回复

package com.cn.servlet;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
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.ServerEndpoint;
import net.sf.json.JSONObject;
@ServerEndpoint("/webSocket/{username}")
public class WebSocket {
private static int onlineCount = 0;
private static Map<String, WebSocket> clients = new ConcurrentHashMap<String, WebSocket>();
private Session session;
private String username;
@onopen
public void onOpen(@PathParam("username") String username, Session session) throws IOException {
this.username = username;
System.out.println(username);
this.session = session;
addOnlineCount();
clients.put(username, this);
System.out.println(clients);
System.out.println("已连接");
}
@onclose
public void onClose() throws IOException {
clients.remove(username);
subOnlineCount();
}
@OnMessage
public void onMessage(String message) throws IOException {
JSONObject jsonTo = JSONObject.fromObject(message);
String mes = (String) jsonTo.get("message");
if (!jsonTo.get("To").equals("All")){
System.out.println("给到"+jsonTo.get("To").toString());
sendMessageTo(mes, jsonTo.get("To").toString());
}else{
sendMessageAll("给所有人");
}
}
@onerror
public void onError(Session session, Throwable error) {
error.printStackTrace();
}
public void sendMessageTo(String message, String To) throws IOException {
System.out.println("进入方法");
// session.getBasicRemote().sendText(message);
//session.getAsyncRemote().sendText(message);
System.out.println(clients.values());
for (WebSocket item : clients.values()) {
System.out.println("进入for循环");
if (item.username.equals(To) )
System.out.println("进入if");
item.session.getAsyncRemote().sendText(message);
}
}
public void sendMessageAll(String message) throws IOException {
for (WebSocket item : clients.values()) {
item.session.getAsyncRemote().sendText(message);
}
}
public static synchronized int getOnlineCount() {
return onlineCount;
}
public static synchronized void addOnlineCount() {
WebSocket.onlineCount++;
}
public static synchronized void subOnlineCount() {
WebSocket.onlineCount--;
}
public static synchronized Map<String, WebSocket> getClients() {
return clients;
}
public static void main(String[] args) {
WebSocket ws = new WebSocket();
JSONObject jo = new JSONObject();
jo.put("message", "这个比密码不对还想登录!");
jo.put("To", "aaaa11");// 给用户名为admin的用户推送
try {
ws.onMessage(jo.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
}


1楼2019-11-21 09:46回复
    用户登录之后会自动通过onOpen这个方法向clients这个对象添加值,可是当触发监听端口进入到sendMessageTo这个方法里,clients这个对象就没有值了变成null了,有咩有大神帮忙看一下


    2楼2019-11-21 09:48
    回复
      2025-11-07 00:07:55
      广告
      不感兴趣
      开通SVIP免广告
      别沉啊,有没有大神帮忙看一下,万分感谢


      3楼2019-11-21 10:01
      回复
        求助啊,大神


        4楼2019-11-21 10:44
        回复
          选这个做毕设就是自找苦头


          来自Android客户端5楼2019-11-21 14:14
          回复
            是不是open之后直接被close了


            IP属地:辽宁6楼2019-11-21 14:33
            回复
              你几处都打断点试试。


              IP属地:辽宁7楼2019-11-21 14:33
              收起回复
                打debug看一下 这种都不会有难度的


                IP属地:四川来自Android客户端9楼2019-11-21 18:38
                回复