private void clearSession(WebSocketSession session, CloseStatus closeStatus) throws Exception {
   if (logger.isDebugEnabled()) {
     logger.debug("Clearing session " + session.getId());
   }
   if (this.sessions.remove(session.getId()) != null) {
     this.stats.decrementSessionCount(session);
   }
   findProtocolHandler(session).afterSessionEnded(session, closeStatus, this.clientInboundChannel);
 }
  @Override
  public void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
    JsonObject jsonMessage = gson.fromJson(message.getPayload(), JsonObject.class);

    log.debug("Incoming message: {}", jsonMessage);

    switch (jsonMessage.get("id").getAsString()) {
      case "start":
        start(session, jsonMessage);
        break;
      case "show_faces":
        setVisualization(session, jsonMessage);
        break;
      case "scale_factor":
        setScaleFactor(session, jsonMessage);
        break;
      case "process_num_frames":
        setProcessNumberFrames(session, jsonMessage);
        break;
      case "width_to_process":
        setWidthToProcess(session, jsonMessage);
        break;
      case "get_stats":
        getStats(session);
        break;
      case "euclidean_dis":
        setEuclideanDistance(session, jsonMessage);
        break;
      case "area_threshold":
        setAreaThreshold(session, jsonMessage);
        break;
      case "track_treshold":
        setTrackThreshold(session, jsonMessage);
        break;
      case "stop":
        {
          UserSession user = users.remove(session.getId());
          if (user != null) {
            user.release();
          }
          break;
        }
      case "onIceCandidate":
        {
          JsonObject candidate = jsonMessage.get("candidate").getAsJsonObject();
          UserSession user = users.get(session.getId());
          if (user != null) {
            user.addCandidate(candidate);
          }
          break;
        }

      default:
        error(session, "Invalid message with id " + jsonMessage.get("id").getAsString());
        break;
    }
  }
 @Override
 public void handleTransportError(WebSocketSession session, Throwable exception)
     throws Exception {
   WebSocketSession removed =
       IntegrationWebSocketContainer.this.sessions.remove(session.getId());
   if (removed != null) {
     IntegrationWebSocketContainer.this.sessions.remove(session.getId());
   }
   throw new Exception(exception);
 }
Ejemplo n.º 4
0
  @Override
  public void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
    JsonObject jsonMessage = gson.fromJson(message.getPayload(), JsonObject.class);

    log.debug("Incoming message: {}", jsonMessage);

    switch (jsonMessage.get("id").getAsString()) {
      case "start":
        start(session, jsonMessage);
        break;
      case "show_mouths":
        setVisualization(session, jsonMessage);
        break;
      case "scale_factor":
        log.debug("Case scale factor");
        setScaleFactor(session, jsonMessage);
        break;
      case "process_num_frames":
        log.debug("Case process num frames");
        setProcessNumberFrames(session, jsonMessage);
        break;
      case "width_to_process":
        log.debug("Case width to process");
        setWidthToProcess(session, jsonMessage);
        break;
      case "stop":
        {
          UserSession user = users.remove(session.getId());
          if (user != null) {
            user.release();
          }
          break;
        }
      case "onIceCandidate":
        {
          JsonObject candidate = jsonMessage.get("candidate").getAsJsonObject();

          UserSession user = users.get(session.getId());
          if (user != null) {
            IceCandidate cand =
                new IceCandidate(
                    candidate.get("candidate").getAsString(),
                    candidate.get("sdpMid").getAsString(),
                    candidate.get("sdpMLineIndex").getAsInt());
            user.addCandidate(cand);
          }
          break;
        }

      default:
        sendError(session, "Invalid message with id " + jsonMessage.get("id").getAsString());
        break;
    }
  }
 /** 关闭连接后 */
 public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus)
     throws Exception {
   System.out.println("Websocket:" + session.getId() + "已经关闭");
   Iterator<Entry<Long, WebSocketSession>> it = userSocketSessionMap.entrySet().iterator();
   // 移除Socket会话
   while (it.hasNext()) {
     Entry<Long, WebSocketSession> entry = it.next();
     if (entry.getValue().getId().equals(session.getId())) {
       userSocketSessionMap.remove(entry.getKey());
       System.out.println("Socket会话已经移除:用户ID" + entry.getKey());
       break;
     }
   }
 }
  public WebSocketServerSession(
      String sessionId,
      Object registerInfo,
      SessionsManager sessionsManager,
      WebSocketSession wsSession) {

    super(sessionId, registerInfo, sessionsManager, wsSession.getId());

    this.wsSession = wsSession;

    this.setRsHelper(
        new JsonRpcRequestSenderHelper(sessionId) {
          @Override
          public <P, R> Response<R> internalSendRequest(Request<P> request, Class<R> resultClass)
              throws IOException {
            return sendRequestWebSocket(request, resultClass);
          }

          @Override
          protected void internalSendRequest(
              Request<? extends Object> request,
              Class<JsonElement> resultClass,
              Continuation<Response<JsonElement>> continuation) {
            sendRequestWebSocket(request, resultClass, continuation);
          }
        });
  }
	@Override
	public void afterConnectionEstablished(WebSocketSession session) throws Exception {
		// WebSocketHandlerDecorator could close the session
		if (!session.isOpen()) {
			return;
		}
		this.stats.incrementSessionCount(session);
		session = new ConcurrentWebSocketSessionDecorator(session, getSendTimeLimit(), getSendBufferSizeLimit());
		this.sessions.put(session.getId(), new WebSocketSessionHolder(session));
		findProtocolHandler(session).afterSessionStarted(session, this.clientInboundChannel);
	}
    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
      session =
          new ConcurrentWebSocketSessionDecorator(
              session,
              IntegrationWebSocketContainer.this.sendTimeLimit,
              IntegrationWebSocketContainer.this.sendBufferSizeLimit);

      IntegrationWebSocketContainer.this.sessions.put(session.getId(), session);
      if (logger.isDebugEnabled()) {
        logger.debug(
            "Started WebSocket session = "
                + session.getId()
                + ", number of sessions = "
                + IntegrationWebSocketContainer.this.sessions.size());
      }
      if (IntegrationWebSocketContainer.this.messageListener != null) {
        IntegrationWebSocketContainer.this.messageListener.afterSessionStarted(session);
      }
    }
 @Override
 public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus)
     throws Exception {
   WebSocketSession removed =
       IntegrationWebSocketContainer.this.sessions.remove(session.getId());
   if (removed != null) {
     if (IntegrationWebSocketContainer.this.messageListener != null) {
       IntegrationWebSocketContainer.this.messageListener.afterSessionEnded(
           session, closeStatus);
     }
   }
 }
	/**
	 * Handle an inbound message from a WebSocket client.
	 */
	@Override
	public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {
		WebSocketSessionHolder holder = this.sessions.get(session.getId());
		if (holder != null) {
			session = holder.getSession();
		}
		SubProtocolHandler protocolHandler = findProtocolHandler(session);
		protocolHandler.handleMessageFromClient(session, message, this.clientInboundChannel);
		if (holder != null) {
			holder.setHasHandledMessages();
		}
		checkSessions();
	}
 @Override
 public void handleMessage(WebSocketSession session, TextMessage message) {
   // The text message should just contain the id of the ballot
   try {
     String payload = message.getPayload();
     Long id = Long.parseLong(payload);
     // now update the hash map
     sessions.put(session, id);
   } catch (Exception e) {
     // We cannot parse the message or other so try to remove the session
     sessions.remove(session.getId());
   }
 }
Ejemplo n.º 12
0
  private void start(final WebSocketSession session, JsonObject jsonMessage) {
    try {
      // Media Logic (Media Pipeline and Elements)
      UserSession user = new UserSession();
      MediaPipeline pipeline = kurento.createMediaPipeline();
      user.setMediaPipeline(pipeline);
      WebRtcEndpoint webRtcEndpoint = new WebRtcEndpoint.Builder(pipeline).build();
      user.setWebRtcEndpoint(webRtcEndpoint);
      users.put(session.getId(), user);

      webRtcEndpoint.addOnIceCandidateListener(
          new EventListener<OnIceCandidateEvent>() {

            @Override
            public void onEvent(OnIceCandidateEvent event) {
              JsonObject response = new JsonObject();
              response.addProperty("id", "iceCandidate");
              response.add("candidate", JsonUtils.toJsonObject(event.getCandidate()));
              try {
                synchronized (session) {
                  session.sendMessage(new TextMessage(response.toString()));
                }
              } catch (IOException e) {
                log.debug(e.getMessage());
              }
            }
          });

      mouth = new NuboMouthDetector.Builder(pipeline).build();

      webRtcEndpoint.connect(mouth);
      mouth.connect(webRtcEndpoint);

      // SDP negotiation (offer and answer)
      String sdpOffer = jsonMessage.get("sdpOffer").getAsString();
      String sdpAnswer = webRtcEndpoint.processOffer(sdpOffer);

      // Sending response back to client
      JsonObject response = new JsonObject();
      response.addProperty("id", "startResponse");
      response.addProperty("sdpAnswer", sdpAnswer);

      synchronized (session) {
        session.sendMessage(new TextMessage(response.toString()));
      }
      webRtcEndpoint.gatherCandidates();

    } catch (Throwable t) {
      sendError(session, t.getMessage());
    }
  }
  private <P, R> Response<R> sendRequestWebSocket(Request<P> request, Class<R> resultClass) {

    log.info("Req-> {}", request.toString());

    Future<Response<JsonElement>> responseFuture = null;

    if (request.getId() != null) {
      responseFuture = pendingRequests.prepareResponse(request.getId());
    }

    try {
      synchronized (wsSession) {
        wsSession.sendMessage(new TextMessage(JsonUtils.toJson(request)));
      }
    } catch (Exception e) {
      throw new KurentoException(
          "Exception while sending message '"
              + JsonUtils.toJson(request)
              + "' to websocket with native sessionId '"
              + wsSession.getId()
              + "'",
          e);
    }

    if (responseFuture == null) {
      return null;
    }

    Response<JsonElement> responseJsonObject;
    try {
      responseJsonObject = responseFuture.get(TIMEOUT, TimeUnit.MILLISECONDS);

      log.info("<-Res {}", responseJsonObject.toString());

    } catch (InterruptedException e) {
      // TODO What to do in this case?
      throw new JsonRpcException("Interrupted while waiting for a response", e);
    } catch (ExecutionException e) {
      // TODO Is there a better way to handle this?
      throw new JsonRpcException("This exception shouldn't be thrown", e);
    } catch (TimeoutException e) {
      throw new TransportException(
          "Timeout of "
              + TIMEOUT
              + " milliseconds waiting from response to request with id:"
              + request.getId(),
          e);
    }

    return MessageUtils.convertResponse(responseJsonObject, resultClass);
  }
  private void start(final WebSocketSession session, JsonObject jsonMessage) {
    try {

      String sessionId = session.getId();
      UserSession user = new UserSession(sessionId);
      users.put(sessionId, user);
      webRtcEndpoint = user.getWebRtcEndpoint();

      // Ice Candidate
      webRtcEndpoint.addOnIceCandidateListener(
          new EventListener<OnIceCandidateEvent>() {
            @Override
            public void onEvent(OnIceCandidateEvent event) {
              JsonObject response = new JsonObject();
              response.addProperty("id", "iceCandidate");
              response.add("candidate", JsonUtils.toJsonObject(event.getCandidate()));
              sendMessage(session, new TextMessage(response.toString()));
            }
          });

      face = new NuboFaceDetector.Builder(user.getMediaPipeline()).build();
      face.activateServerEvents(1, 3000);
      addFaceListener();

      webRtcEndpoint.connect(face);
      face.connect(webRtcEndpoint);

      // SDP negotiation (offer and answer)
      String sdpOffer = jsonMessage.get("sdpOffer").getAsString();
      String sdpAnswer = webRtcEndpoint.processOffer(sdpOffer);

      // Sending response back to client
      JsonObject response = new JsonObject();
      response.addProperty("id", "startResponse");
      response.addProperty("sdpAnswer", sdpAnswer);

      synchronized (session) {
        sendMessage(session, new TextMessage(response.toString()));
      }
      webRtcEndpoint.gatherCandidates();

    } catch (NotEnoughResourcesException e) {
      log.warn("Not enough resources", e);
      notEnoughResources(session);
    } catch (Throwable t) {
      log.error("Exception starting session", t);
      error(session, t.getClass().getSimpleName() + ": " + t.getMessage());
    }
  }
 /** 消息传输错误处理 */
 public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
   if (session.isOpen()) {
     session.close();
   }
   Iterator<Entry<Long, WebSocketSession>> it = userSocketSessionMap.entrySet().iterator();
   // 移除Socket会话
   while (it.hasNext()) {
     Entry<Long, WebSocketSession> entry = it.next();
     if (entry.getValue().getId().equals(session.getId())) {
       userSocketSessionMap.remove(entry.getKey());
       System.out.println("Socket会话已经移除:用户ID" + entry.getKey());
       break;
     }
   }
 }
 @Override
 public void destroy() throws Exception {
   try {
     // Notify sessions to stop flushing messages
     for (WebSocketSession session : this.sessions.values()) {
       try {
         session.close(CloseStatus.GOING_AWAY);
       } catch (Exception e) {
         logger.error("Failed to close session id '" + session.getId() + "': " + e.getMessage());
       }
     }
   } finally {
     this.sessions.clear();
   }
 }
Ejemplo n.º 17
0
 public UserSession getBySession(WebSocketSession session) {
   return usersBySessionId.get(session.getId());
 }
 @Override
 public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
   log.info("Closed websocket connection of session {}", session.getId());
   release(session);
 }
 private void release(WebSocketSession session) {
   UserSession user = users.remove(session.getId());
   if (user != null) {
     user.release();
   }
 }
Ejemplo n.º 20
0
 public UserSession removeBySession(WebSocketSession session) {
   final UserSession user = getBySession(session);
   // usersByName.remove(user.getName());
   usersBySessionId.remove(session.getId());
   return user;
 }