Example usage for org.springframework.web.socket WebSocketSession toString

List of usage examples for org.springframework.web.socket WebSocketSession toString

Introduction

In this page you can find the example usage for org.springframework.web.socket WebSocketSession toString.

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the object.

Usage

From source file:cn.com.inhand.devicenetworks.ap.websocket.WSDNAccessPoint.java

@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
    System.out.println("Debug in WSDNAccessPoint.java [Ln:50] : WebSocketSession session=" + session.toString()
            + " is colsed. stauts=" + status);

    //----call ?api websocket
    WSDNSession wsdnsn = this.cinfo.getWsdnsn(session.toString());
    if (wsdnsn != null) {
        wsdnsn.setSession(session);//from   w w  w. java2  s.c o m
        wsdnsn.setLast_msg(System.currentTimeMillis());
        this.cinfo.getWssn_map().remove(wsdnsn.getId());
        wsdnsn.setIsLogin(false);
        wsdnsn.setSession(null);
        /**
         * status.getCode() 1000 1001?  
         *
         */
        int status_code = status.getCode();
        if (status_code == 1000) {
            if (wsdnsn.getAction() >= 3) {
                this.updateStatus(wsdnsn.getAction(), wsdnsn);
            } else {
                this.updateStatus(202, wsdnsn);
            }
        } else {
            if (wsdnsn.getAction() >= 3) {
                this.updateStatus(wsdnsn.getAction(), wsdnsn);
            } else {
                this.updateStatus(202, wsdnsn);
            }
        }
    }
    //mapsession
    this.cinfo.getWsdnsn_map().remove(session.toString());

    super.afterConnectionClosed(session, status);
}

From source file:cn.com.inhand.devicenetworks.ap.websocket.WSDNAccessPoint.java

@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {

    System.out//w  w  w  .j a  v a2 s  .  c om
            .println("Debug in WSDNAccessPoint.java [Ln:81] : WebSocketSession session=" + session.toString());

    super.handleTextMessage(session, message);
    TextMessage returnMessage = new TextMessage(message.getPayload() + " received at server");
    //session.sendMessage(returnMessage);
    System.out.println("Debug in WSDNAccessPoint.java [Ln:57] : WebSocketSession recv:" + message.getPayload());

    try {
        String string = message.getPayload();
        if (string == null || string.trim().equals("") || string.equals("undefined")) {
            System.out.println("Debug in WSDNAccessPoint.java [Ln:64] : WebSocketSession recv:null msg");

            return;
        }
        DNMessage msg = parser.unwrap(message.getPayload().getBytes());

        WSDNSession wsdnsn = this.cinfo.getWsdnsn(session.toString());

        if (wsdnsn == null) {
            if (msg.getName().equals("login")) {
                try {
                    this.onLogin(msg, session, wsdnsn);
                } catch (PacketException pe) {
                    //
                    Logger.getLogger(WSDNAccessPoint.class.getName()).warning(
                            "Failed to Login from " + session.getRemoteAddress() + ":" + pe.toString());
                    this.close(session);
                }
            } else {
                //?login
                Logger.getLogger(WSDNAccessPoint.class.getName()).warning(
                        "Illegal connection from + " + session.getRemoteAddress() + ", the msg:" + string);
                this.close(session);
            }
        } else if (!wsdnsn.isIsLogin()) {
            wsdnsn.setSession(session);
            try {

                this.onLogin(msg, session, wsdnsn);
            } catch (PacketException pe) {
                //
                Logger.getLogger(WSDNAccessPoint.class.getName())
                        .warning("Failed to Login from " + session.getRemoteAddress() + ":" + pe.toString());
                this.close(session);
            }
        } else {
            //?

            if (msg.getName().equalsIgnoreCase("heartbeat") && msg.getType() == 0) {
                this.onHeartbeat(msg, session, wsdnsn);

                this.updateStatus(2, wsdnsn);
            } else if (msg.getName().equalsIgnoreCase("logout")) {//&& msg.getType() == 0) {
                this.onLogout(msg, session, wsdnsn);
                session.close();
            } else if (msg.getType() == 1) {
                this.onAck(msg, session, wsdnsn);
            } else {
                this.onUnkownMsg(msg, session, wsdnsn);
                Logger.getLogger(WSDNAccessPoint.class.getName())
                        .warning("Unsupported msg from " + session.getRemoteAddress() + ":" + msg.toString());
                //this.close(session);
            }

        }

    } catch (PacketException ex) {
        Logger.getLogger(WSv1Processor.class.getName()).log(Level.SEVERE, "while handling a text message", ex);
    }

}

From source file:cn.com.inhand.devicenetworks.ap.websocket.WSDNAccessPoint.java

/**
 * ?Inbox/*from w  ww  . ja  v a2 s .c om*/
 *
 * @param login
 */
private void onLogin(DNMessage login, WebSocketSession session, WSDNSession wsdnsn)
        throws PacketException, IOException {
    if (login.getName().equals("login") && login.getType() == 0) {
        int result = auth(login);
        //API??
        if (result != 0) {
            List list = new ArrayList();
            list.add(new Parameter("result", "" + result));
            list.add(new Parameter("reason", ""));
            DNMessage ack = new DNMessage("login", "response", login.getTxid(), list);
            session.sendMessage(new TextMessage(new String(parser.wrap(ack))));
            list.clear();

            throw new PacketException("Failed to Login!");
        } else {
            //for debug
            if (login.getParameter("id").getValue().equals("1111")) {

                List list = new ArrayList();
                list.add(new Parameter("result", "21336"));
                list.add(new Parameter("reason", ""));
                DNMessage ack = new DNMessage("login", "response", login.getTxid(), list);
                session.sendMessage(new TextMessage(new String(parser.wrap(ack))));
                list.clear();

                throw new PacketException("The token is invalid!");
            } else {
                List list = new ArrayList();
                list.add(new Parameter("result", "0"));
                list.add(new Parameter("reason", ""));
                DNMessage ack = new DNMessage("login", "response", login.getTxid(), list);
                session.sendMessage(new TextMessage(new String(parser.wrap(ack))));
                list.clear();
                wsdnsn = new WSDNSession(login, session);
                wsdnsn.setAction(1);
                wsdnsn.setId(login.getParameter("id").getValue());
                wsdnsn.setIsLogin(true);
                wsdnsn.setAssetid(login.getParameter("asset_id").getValue());
                wsdnsn.setSn(login.getParameter("sn").getValue());
                wsdnsn.setToken(login.getParameter("access_token").getValue());

                wsdnsn.setKey(login.getParameter("key").getValue());
                wsdnsn.setId(login.getParameter("id").getValue());
                wsdnsn.setConnection_time(System.currentTimeMillis());
                wsdnsn.setLast_msg(wsdnsn.getConnection_time());

                //map
                this.cinfo.putWsdnsn(session.toString(), wsdnsn);
                try {
                    WebSocketSession oldSession = this.cinfo.getWssn(wsdnsn.getId());
                    if (oldSession != null && oldSession.isOpen()) {
                        List list1 = new ArrayList();
                        list1.add(new Parameter("result", "23010"));
                        list1.add(new Parameter("reason", "A new session is established"));
                        DNMessage logout = new DNMessage("logout", "request", "MSG_FROM_SMARTVMS-1", list1);
                        oldSession.sendMessage(new TextMessage(new String(parser.wrap(logout))));
                        oldSession.close();
                    }
                } catch (Exception e) {

                }
                this.cinfo.putWssn(wsdnsn.getId(), session);
                //this.isLogin = true;
            }
        }

    } else {
        throw new PacketException("The Packet is not a login packet!");
    }
}