应用程序架构应该如何,如果我们想使用Continental-jersey for websockets?

时间:2014-07-04 05:43:59

标签: java angularjs atmosphere

我是新手,并试图理解&为我的POC实施大气球衣:TODO app。我不确定我的架构是对还是错。想知道我的Web应用程序的架构怎么样。

客户端我使用angular.js和服务器只是大气球衣。

我的TODO应用包含,Teams& TODO' S。

  • 如果有人创建了团队,则应将其推送/广播给所有已记录的用户
  • 如果有人在团队中创建TODO项目,则应仅向团队成员
  • 广播

首先使用LoginService

  • 客户端向LoginLogoutService发出第一个请求,用于检查用户凭据。在这里,我可以使用简单的http请求,并且可以在HttpSession中保存userObject,但是对于AtmosphereServices,存储在HttpSession中的对象不可用。所以我正在使用atmosphere-websocket-request。

    var request = {
    url: 'atm/loginLogoutService/login?' + queryString,
    contentType: 'application/json',
    logLevel: 'debug',
    transport: 'websocket',
    trackMessageLength: true,
    shared: true,
    fallbackTransport: 'long-polling'
    };
    
  • 如果他是授权用户,那么在暂停方法中我创建会话(通过将其uuid存储在ConcurrentMap中)并设置属性:atmosphereResource中的userObject

    @Path("login")
    @Suspend(contentType = "application/json", listeners = {OnDisconnect.class})
    @GET
    public String suspend(
                    @Context AtmosphereResource resource,
                    @QueryParam("sessionId") String sessionId,
                    @QueryParam("userId") int userId,
                    @QueryParam("password") String password,
                    @QueryParam("fullname") String fullName
    ) {
            try {
                    UserManager um = new UserManager();
                    UserData loginAtemptByUser = null;
                    if (fullName == null || "".equalsIgnoreCase(fullName)) {
                            logger.debug("Checking from DB...");
                            loginAtemptByUser = um.authenticateUser(userId, password);
                            if (loginAtemptByUser != null) {
                                    logger.debug("Authorised user logged in.");
                                    createSession(resource);
                                    setAttribute(resource, USER_IN_SESSION_ATTR, loginAtemptByUser);
                                    responseJSON.put(KEY_LOGIN_STATUS, VAL_LOGIN_SUCCESS);
                                    responseJSON.put(ATMOSPHERE_UUID, resource.uuid());
                                    responseJSON.put(KEY_REQ_MSG, loginAtemptByUser.getFullName());
                            } else {
                                    logger.debug("No match found with username & password");
                            }
                            responseJSON.put(KEY_REQ_STATUS, VAL_REQ_STATUS_SUCCESS);
                    }
            } catch (NumberFormatException e) {
                    responseJSON.put(KEY_REQ_STATUS, VAL_REQ_STATUS_ERROR);
                    responseJSON.put(KEY_REQ_MSG, "Invalid data, please check");
            } catch (JSONException e) {
                    responseJSON.put(KEY_REQ_STATUS, VAL_REQ_STATUS_ERROR);
                    responseJSON.put(KEY_REQ_MSG, "Error occured, please try again later");
            }
            return responseJSON.toString();
    }
    
  • 在客户端,我可以在onMessage函数中获取responseJSON,我可以做必要的事情。

    request.onMessage = function(response) {
        console.info("onMessage : " + 'response.responseBody: ' + response.responseBody)
    }
    
  • 现在,当用户点击“团队”标签时,我再次通过发送从loginLogoutService获取的uuid来制作氛围Web套接字请求,如下所示。 (我们可以使用现有的吗?如果是,怎么做?)

    var request = {
    url: 'atm/teamsService?uuid='+$rootScope.uuid, //this is angularJs, uuid is stored in a variable called "$rootScope.uuid"
    contentType: 'application/json',
    logLevel: 'debug',
    transport: 'websocket',
    trackMessageLength: false,
    reconnectInterval: 5000,
    enableXDR: true,
    timeout: 60000,
    fallbackTransport: 'long-polling',
    };
    
  • 在TeamsService,suspend方法中,我可以获取uuid,可以检查它是否存在于ConcurrentMap中。如果存在,则发送回之前创建的所有团队。

    @Suspend(contentType = "application/json", listeners = {OnDisconnect.class})
    @GET
    public String suspend(
                    @QueryParam("uuid") String uuid
    ) {
    
            logger.info("Request to suspend for Teams with uuid - " + uuid);
            if (isSessionInitialized(uuid)) {
                    logger.info("Retriving All teams for a newly connected user");
                    List<Teams> list = new TeamsManager().retriveAllTeams();
    
                    JSONArray jsonArray = new JSONArray();
                    JSONObject teams = null;
                    JSONObject teamsWithMembersCount = null;
                    for (Teams team : list) {
                            teams = new JSONObject();
                            teamsWithMembersCount = new JSONObject();
                            teams.put("teamId", team.getTeamId());
                            teams.put("teamName", team.getTeamName());
                            teamsWithMembersCount.put("membersCount", 0);
                            teamsWithMembersCount.put("teams", teams);
                            jsonArray.put(teamsWithMembersCount);
                    }
                    return jsonArray.toString();
            } else {
                    logger.info("uuid is not in atmosphere session");
                    return "";
            }
    }
    
  • 现在,当用户从UI创建团队时,我将数据推送到服务器广播器

    $scope.addTeam = function() {
       socket.push(jQuery.stringifyJSON({teamId: 1, teamName: $scope.newTeam}));
       $scope.newTeam = "";
    };
    
  • 以下是我在TeamsService的广播公司

    @Broadcast(writeEntity = true)
    @POST
    @Produces("application/json")
    public TeamsResponse broadcast(
                    @QueryParam("uuid") String uuid,
                    Teams team) {
            printSessionsByUuidMap();
            logger.debug("Request to create new team - " + uuid);
            UserData ud = null;
            TeamsResponse teamRes = null;
            try {
                    ud = (UserData) getAttribute(uuid, USER_IN_SESSION_ATTR);
                    logger.debug("session------->>>> " + ud.getFullName());
            } catch (Exception e) {
                    e.printStackTrace();
            }
            if (ud != null) {
                    TeamsManager tm = new TeamsManager();
                    teamRes = new TeamsResponse(team, 0);
                    Teams teamToSave = new Teams();
                    teamToSave.setTeamName(team.getTeamName());
                    if (tm.createTeam(teamToSave, ud)) {
                            teamRes = new TeamsResponse(team, 0);
                    }
            }else{
                    logger.debug("Could'nt get current user from atmosphere session ");
            }
    
            return teamRes;
    }
    

一切正常。但我不知道,我是做对还是错

提前致谢

1 个答案:

答案 0 :(得分:0)

鉴于存在option to use/track sessions in Atmosphere,我建议您不要将WebSockets用于与服务器的所有交易,仅用于服务器端通知。