Пример #1
0
  @Override
  protected void onTextMessage(CharBuffer message) throws IOException {
    logger.debug("OnText - " + message);
    ObjectMapper mapper = new ObjectMapper();
    Command cmd;
    try {
      cmd = mapper.readValue(message.toString(), Command.class);
    } catch (Exception e) {
      e.printStackTrace();
      return;
    }

    if (!validateCommand(cmd)) {
      return;
    }

    if (cmd != null) {
      logger.debug(cmd.toString());
    }

    switch (cmd.getActionType()) {
      case Command.SUBSCRIBE:
        ((WebSocketPool) SpringContextUtil.getBean("webSocketPool")).bind(cmd.getChannel(), this);
        writeMessage(1, Command.SUBSCRIBE, "订阅成功!");
        break;
      case Command.UNSUBSCRIBE:
        ((WebSocketPool) SpringContextUtil.getBean("webSocketPool")).unbind(cmd.getChannel(), this);
        writeMessage(1, Command.SUBSCRIBE, "取消订阅成功!");
        break;
      case Command.PUBLISH:
        for (String channel : cmd.getChannel()) {
          ((PubSubService) SpringContextUtil.getBean("pubSubService"))
              .publish(channel, cmd.getMessage());
        }
        writeMessage(1, Command.PUBLISH, "发布成功!");
        break;
      case Command.HEART_BEAT:
        writeMessage(1, Command.HEART_BEAT, "链接成功!");
        break;
      default:
        writeMessage(-1, cmd.getActionType(), "未知命令!");
        break;
    }
  }
Пример #2
0
 @Override
 protected void onClose(int status) {
   logger.debug("WebSocket onClose");
   ((WebSocketPool) SpringContextUtil.getBean("webSocketPool")).unbind(this);
 }