com.oic.net.WebSocketListener.java Source code

Java tutorial

Introduction

Here is the source code for com.oic.net.WebSocketListener.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.oic.net;

import com.oic.client.OicCharacter;
import com.oic.connection.Connections;
import com.oic.event.CheckDuplication;
import com.oic.event.PosUpdate;
import com.oic.event.RegisterProfile;
import com.oic.login.LoginHandler;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

/**
 *
 * @author morimoto
 */
@WebSocket
public class WebSocketListener {
    private static final Logger LOG = Logger.getLogger(WebSocketListener.class.getName());
    private Session session;
    private OicCharacter c = null;//,??????NULL

    /**
     * NOLOGIN  ?????
     * LOGIN    ?
     * REGISTER ????
     */
    public enum LoginStatus {
        /**
         * 
         */
        NOLOGIN,
        /**
         * ???
         */
        LOGIN,
        /**
         * ?
         */
        REGISTER,
    }

    private LoginStatus login = LoginStatus.NOLOGIN;

    @OnWebSocketConnect
    public void onConnect(Session session) {
        login = LoginStatus.NOLOGIN;
        this.session = session;
        Connections.addConnection(this);//??

        LOG.log(Level.INFO, "connection :{0}", session.getRemoteAddress());//Logging 
    }

    @OnWebSocketMessage
    public void onText(String message) {
        String method = "";
        JSONObject json = new JSONObject();
        try {
            json = (JSONObject) (new JSONParser().parse(message));
            LOG.log(Level.INFO, "method : {0}", json.get("method"));
            //LOG.log(Level.INFO, "status : {0}", login);
            method = json.get("method").toString();
        } catch (ParseException e) {
            LOG.log(Level.WARNING, "{0}", e);
        }

        /* ?  */
        selectMessage(method, json, this);
    }

    @OnWebSocketClose
    public void onClose(int statusCode, String reason) {
        userLogout();
        LOG.log(Level.INFO, "close statusCode = {0} reason = {1}", new Object[] { statusCode, reason });
    }

    /**
     * ????
     *
     * @param method
     * @param json
     * @param webSocketListener
     */
    public void selectMessage(String method, JSONObject json, WebSocketListener webSocketListener) {
        if (method == null) { //??????
            LOG.log(Level.WARNING, "Message Method is NULL : {0}", json.toString());
            return;
        } else if (method.equals("cmd")) {
            //   new CmdEvent().ActionEvent(json, webSocketListener);
        } else if ((login == LoginStatus.NOLOGIN || login == LoginStatus.REGISTER)
                && method.equals("duplication")) {
            /* ????Status?REGISTER?? */
            new CheckDuplication().ActionEvent(json, webSocketListener);
        } else if (login == LoginStatus.NOLOGIN && method.equals("login")) {
            /*  */
            new LoginHandler().ActionEvent(json, webSocketListener);
            //new LoginHandler().ActionEvent(json, this);//?
        } else if (login == LoginStatus.REGISTER && method.equals("setprofile")) {
            /* ????? */
            new RegisterProfile().ActionEvent(json, webSocketListener);
        } else if (login == LoginStatus.LOGIN) {
            LoginEvent.execEvent(method, json, webSocketListener);
        }
    }

    public void sendJson(JSONObject json) {
        try {
            session.getRemote().sendString(json.toJSONString());
        } catch (IOException e) {
            LOG.log(Level.WARNING, "send Json error{0}", e);
        }
    }

    public OicCharacter getCharacter() {
        return c;
    }

    public void setCharacter(OicCharacter character) {
        this.c = character;
    }

    public Session getSession() {
        return this.session;
    }

    /**
     * ?
     * @param userId 
     */
    public void userLogin(long userId) {
        login = LoginStatus.LOGIN;
        c = OicCharacter.loadCharFromDB(userId);
        if (c == null) {//
            login = LoginStatus.REGISTER;
        }
        c.loginMap(31); //??3A

        //?
        new PosUpdate().ActionEvent(null, this);
    }

    /**
     * ???????
     */
    public void userNoLogin() {
        login = LoginStatus.NOLOGIN;
    }

    /**
     * ?
     */
    public void userLogout() {
        login = LoginStatus.NOLOGIN;
        try {
            c.getMap().removeCharacter(c.getUserId());
        } catch (NullPointerException e) {
        }
        Connections.removeConnection(this);
    }

    /**
     * 
     */
    public void userRegister() {
        login = LoginStatus.REGISTER;
    }

    /**
     * ?????
     * @return 
     */
    public boolean isLogin() {
        return (login == LoginStatus.LOGIN);
    }

    /**
     * ??
     * @return 
     */
    public boolean isRegister() {
        return (login == LoginStatus.REGISTER);
    }
}