Package org.auto.comet

Examples of org.auto.comet.Socket


  public void addSocket(PushSocket socket) {
    this.sockets.put(socket.getId(), socket);
  };

  public boolean hasSocket(Serializable id) {
    Socket socket = this.getSocket(id);
    return null != socket;
  }
View Full Code Here


  }

  @Override
  public void quit(Socket socket, HttpServletRequest request) {
    for (Entry<Serializable, Socket> entry : socketMapping.entrySet()) {
      Socket value = entry.getValue();
      if (value.equals(socket)) {
        Serializable key = entry.getKey();
        socketMapping.remove(key);
      }
    }
  }
View Full Code Here

    socketMapping.remove(userId);
  }

  @Override
  public void login(Serializable userId) {
    Socket userSocket = socketMapping.get(userId);
    // 登录了给所有在线的人发广播
    for (Entry<Serializable, Socket> entry : socketMapping.entrySet()) {
      Serializable id = entry.getKey();
      Socket socket = entry.getValue();
      if (id.equals(userId)) {
        continue;
      }
      JSONObject message = new JSONObject();
      message.put(COMMAND_KEY, COMMAND_LOGIN);
      message.put("userId", userId);
      socket.send(message.toString());

      JSONObject message2 = new JSONObject();
      message2.put(COMMAND_KEY, COMMAND_LOGIN);
      message2.put("userId", id);
      userSocket.send(message2.toString());
View Full Code Here

  }

  @Override
  public void sendMessage(String userId, String targetUserId, String message) {
    Socket socket = socketMapping.get(targetUserId);
    JSONObject result = new JSONObject();
    result.put(COMMAND_KEY, COMMAND_RECEIVE);
    result.put("userId", userId);
    result.put("text", message);
    String now = DateFormatUtils.format(new Date(), "HH:mm:ss");
    result.put("time", now);
    socket.send(result.toString());
  }
View Full Code Here

  }

  @Override
  public void sendMessage(Serializable[] userIds, String message) {
    for (Serializable userId : userIds) {
      Socket socket = socketMapping.get(userId);
      socket.send(message);
    }
  }
View Full Code Here

  private void romveSocket(Socket socket) {
    writeLock.lock();
    try {
      Serializable key = null;
      for (Entry<Serializable, Socket> entry : socketMapping.entrySet()) {
        Socket value = entry.getValue();
        if (value.equals(socket)) {
          key = entry.getKey();
          break;
        }
      }
      if (key != null) {
View Full Code Here

  }

  public void send(Serializable id, String msg) {
    readLock.lock();
    try {
      Socket socket = this.socketMapping.get(id);
      if (null != socket) {
        socket.send(msg);
      } else {
        throw new RuntimeException("Can't find socket!");
      }
    } finally {
      readLock.unlock();
View Full Code Here

  public void sendExcept(Serializable exceptId, String msg) {
    readLock.lock();
    try {
      for (Entry<Serializable, Socket> entry : socketMapping.entrySet()) {
        Serializable id = entry.getKey();
        Socket socket = entry.getValue();
        if (id.equals(exceptId)) {
          continue;
        }
        socket.send(msg);
      }
    } finally {
      readLock.unlock();
    }
  }
View Full Code Here

  public void sendToAll(String msg) {
    readLock.lock();
    try {
      for (Entry<Serializable, Socket> entry : socketMapping.entrySet()) {
        Socket socket = entry.getValue();
        socket.send(msg);
      }
    } finally {
      readLock.unlock();
    }
  }
View Full Code Here

  private void romveSocket(Socket socket) {
    writeLock.lock();
    try {
      for (Entry<Serializable, Socket> entry : socketMapping.entrySet()) {
        Socket value = entry.getValue();
        if (value.equals(socket)) {
          Serializable key = entry.getKey();
          socketMapping.remove(key);
        }
      }
    } finally {
View Full Code Here

TOP

Related Classes of org.auto.comet.Socket

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.