de.hsbremen.websocketChat.WebSocketSession.java Source code

Java tutorial

Introduction

Here is the source code for de.hsbremen.websocketChat.WebSocketSession.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 de.hsbremen.websocketChat;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.eclipse.jetty.websocket.WebSocket;
import org.json.*;

/**
 *
 * @author geno
 */
class WebSocketSession implements WebSocket.OnTextMessage {

    private Connection connection;
    private User user;

    public WebSocketSession() {
    }

    @Override
    public void onMessage(String data) {
        JSONObject recv = new JSONObject(data);

        if (recv.has("disconnect")) {
            connection.close();
        } else {
            if (recv.has("status")) {
                try {
                    connection.sendMessage((new JSONObject()).put("login", (user != null) ? "1" : "0").toString());
                } catch (IOException ex) {
                    Logger.getLogger(WebSocketSession.class.getName()).log(Level.SEVERE, null, ex);
                }
            } else if (user != null) {
                if (recv.has("changeUser")) {
                    ServletWebSocket.msgs.add(
                            new Message(user.getName(), "change name to \"" + recv.getString("changeUser") + "\""));
                    user.setName(recv.getString("changeUser"));
                } else if (recv.has("msg")) {
                    ServletWebSocket.msgs.add(new Message(user.getName(), recv.getString("msg")));
                    ServletWebSocket.onNotifyAll();
                }
            } else {
                if (recv.has("setUser")) {
                    try {
                        user = new User(recv.getString("setUser"));
                        ServletWebSocket.msgs.add(new Message(user.getName(), "auto hallo"));
                        connection.sendMessage(sendCurrentMessages().put("login", "1").toString());
                    } catch (IOException ex) {
                        Logger.getLogger(WebSocketSession.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        }
    }

    @Override
    public void onOpen(Connection connection) {
        this.connection = connection;
    }

    @Override
    public void onClose(int i, String string) {
        ServletWebSocket.wss.remove(this);
    }

    public void onNotify() {
        try {
            connection.sendMessage(sendCurrentMessages().toString());
        } catch (IOException ex) {
            Logger.getLogger(WebSocketSession.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private JSONObject sendCurrentMessages() {
        JSONArray msgs = new JSONArray();
        for (Message msg : ServletWebSocket.msgs) {
            msgs.put(new JSONObject().put("user", msg.getUser()).put("text", msg.getText()));
        }
        return new JSONObject().put("msgs", msgs);
    }

}