Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.Map;
- import java.util.Set;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.websocket.EncodeException;
- import javax.websocket.Session;
- import org.json.simple.JSONObject;
- import org.json.simple.parser.JSONParser;
- import org.json.simple.parser.ParseException;
- /*
- * 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.
- */
- /**
- *
- * @author student_10
- */
- public class SessionHandler {
- static int i = 0;
- static int licznik = 0;
- static int licznikSES =0;
- static String [] sesje = new String[4];
- static int[] wartosci = new int[2];
- static int losowa = 1 + (int)(Math.random() * 10);
- static int losowa2 = 1 + (int)(Math.random() * 10);
- int x=0;
- static String losowaSTR;
- static String losowaSTR2;
- private static final Set<Session> sessions = new HashSet<>();
- static Map<String, Session> openSessions = new HashMap();
- static Map<String, Session> klienci = new HashMap();
- static JSONObject jsonObject = new JSONObject();
- static JSONObject jsonObject2 = new JSONObject();
- public static void addSession(Session session){
- sesje[licznikSES] = session.getId();
- licznikSES++;
- sessions.add(session);
- }
- public static void removeSession(Session session){
- sessions.remove(session);
- }
- public static void sendToSession(Session session, String message){
- String janek = "janusz";
- i++;
- losowaSTR = String.valueOf(losowa);
- losowaSTR2 = String.valueOf(losowa2);
- //System.out.println(licznikSES);
- try {
- if(licznikSES == 4 ) {
- JSONParser parser = new JSONParser();
- try {
- Object obj = parser.parse(message);
- JSONObject jsonObj = (JSONObject) obj;
- if(jsonObj.get("Info").equals("gotowy")) {
- JSONObject wysylkaJson = new JSONObject();
- for (Session s : sessions) {
- if (s.isOpen()&& s.getId().equals(sesje[0])) {
- wysylkaJson.put("Info", "start");
- wysylkaJson.put("Gracz", "0");
- s.getBasicRemote().sendObject(wysylkaJson);
- //s.getBasicRemote().sendObject(0);
- //s.getBasicRemote().sendObject("start");
- }
- if (s.isOpen()&& s.getId().equals(sesje[1])) {
- wysylkaJson.put("Info", "start");
- wysylkaJson.put("Gracz", "1");
- s.getBasicRemote().sendObject(wysylkaJson);
- //s.getBasicRemote().sendObject(1);
- //s.getBasicRemote().sendObject("start");
- }
- if (s.isOpen()&& s.getId().equals(sesje[2])) {
- wysylkaJson.put("Info", "start");
- wysylkaJson.put("Gracz", "2");
- s.getBasicRemote().sendObject(wysylkaJson);
- //s.getBasicRemote().sendObject(2);
- //s.getBasicRemote().sendObject("start");
- }
- if (s.isOpen()&& s.getId().equals(sesje[3])) {
- wysylkaJson.put("Info", "start");
- wysylkaJson.put("Gracz", "3");
- s.getBasicRemote().sendObject(wysylkaJson);
- //s.getBasicRemote().sendObject(3);
- //s.getBasicRemote().sendObject("start");
- }
- }
- }
- else if (jsonObj.get("Info").equals("ruch")) {
- for (Session s : sessions) {
- if (s.isOpen()) {
- if(jsonObj.get("Gracz").equals("0")) {
- s.getBasicRemote().sendObject(message);
- }
- if(jsonObj.get("Gracz").equals("1")) {
- s.getBasicRemote().sendObject(message);
- }
- if(jsonObj.get("Gracz").equals("2")) {
- s.getBasicRemote().sendObject(message);
- }
- if(jsonObj.get("Gracz").equals("3")) {
- s.getBasicRemote().sendObject(message);
- }
- }
- }
- } else if (jsonObj.get("Info").equals("strzal")) {
- }
- else if (jsonObj.get("Info").equals("koniec")) {
- }
- } catch (ParseException ex) {
- Logger.getLogger(SessionHandler.class.getName()).log(Level.SEVERE, null, ex);
- }
- /* if (message.equals("halo")){
- for (Session s : sessions) {
- if (s.isOpen()&& s.getId().equals(sesje[0])) {
- s.getBasicRemote().sendObject(0);
- s.getBasicRemote().sendObject("start");
- }
- if (s.isOpen()&& s.getId().equals(sesje[1])) {
- s.getBasicRemote().sendObject(1);
- s.getBasicRemote().sendObject("start");
- }
- if (s.isOpen()&& s.getId().equals(sesje[2])) {
- s.getBasicRemote().sendObject(2);
- s.getBasicRemote().sendObject("start");
- }
- if (s.isOpen()&& s.getId().equals(sesje[3])) {
- s.getBasicRemote().sendObject(3);
- s.getBasicRemote().sendObject("start");
- }
- }
- }
- else {
- for (Session s : sessions) {
- if (s.isOpen()) {
- System.out.println(message);
- //JSONParser parser = new JSONParser();
- try {
- Object obj = parser.parse(message);
- JSONObject jsonObj = (JSONObject) obj;
- if(jsonObj.get("ID").equals("0")) {
- s.getBasicRemote().sendObject(message);
- }
- if(jsonObj.get("ID").equals("1")) {
- s.getBasicRemote().sendObject(message);
- }
- if(jsonObj.get("ID").equals("2")) {
- s.getBasicRemote().sendObject(message);
- }
- if(jsonObj.get("ID").equals("3")) {
- s.getBasicRemote().sendObject(message);
- }
- } catch (ParseException ex) {
- // Logger.getLogger(SessionHandler.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
- }*/
- }
- } catch (IOException ex) {
- sessions.remove(session);
- Logger.getLogger(SessionHandler.class.getName()).log(Level.SEVERE, null, ex);
- } catch (EncodeException ex) {
- Logger.getLogger(SessionHandler.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- public static void sendToallConnectedSessions(String message){
- for (Session session : sessions) {
- sendToSession(session, message);
- }
- }
- public static void sendToallConnectedSessionsInRoom(String roomID, String message){
- for (Session session : sessions) {
- sendToSession(session, message);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement