Skip to content

Instantly share code, notes, and snippets.

@ilev4ik
Created March 10, 2017 19:27
Show Gist options
  • Select an option

  • Save ilev4ik/34e69a37651dbc3fd663b2875a2b4c04 to your computer and use it in GitHub Desktop.

Select an option

Save ilev4ik/34e69a37651dbc3fd663b2875a2b4c04 to your computer and use it in GitHub Desktop.

Revisions

  1. ilev4ik created this gist Mar 10, 2017.
    121 changes: 121 additions & 0 deletions VisualMathSync.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,121 @@
    import io.socket.client.Socket;
    import io.socket.client.IO;
    import io.socket.emitter.Emitter;
    import org.json.JSONException;
    import org.json.JSONObject;

    import java.net.URISyntaxException;
    import com.google.gson.*;

    // sync_v1/ongoing_lectures/set_slide:${activeLectureId} -- slide
    // sunc_v1/lectures/finish
    // sync_v1/questions:${activeLectureId}:${questionId} -- какая-то херня start, finish
    // `sync_v1/blocks:${activeLectureId}:${blockId}` -- block start, finish

    public class VisualMathSync {
    private Socket socket;

    private String state;
    private String ongoingId;

    public String getState() {
    return state;
    }

    public String getOngoingId() {
    return ongoingId;
    }

    public static void main(String[] args) {
    try {
    new VisualMathSync();
    } catch (Exception e) {
    e.printStackTrace();
    }

    }

    public VisualMathSync() throws URISyntaxException {
    IO.Options opts = new IO.Options();
    opts.forceNew = true;
    opts.path = "/ws";

    socket = IO.socket("http://sync.visualmath.ru", opts);

    socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {

    @Override
    public void call(Object... args) {
    System.out.println("Connected");
    }

    }).on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {
    @Override
    public void call(Object... objects) {
    System.out.println("Disconnected");
    }
    }).on("sync_v1/lectures", new Emitter.Listener() {
    @Override
    public void call(Object... objects) {
    System.out.println("Got lecture event");
    Gson gson = new Gson();


    String json = ((JSONObject)objects[0]).toString();
    System.out.print(json);
    //Lecture lec = gson.fromJson(json, Lecture.class);
    //System.out.print(lec.toString());
    addSlideHandler();
    }
    });


    socket.connect();
    }


    private void addSlideHandler() {
    socket.on("sync_v1/ongoing_lectures/set_slide:" + getOngoingId() , new Emitter.Listener() {
    @Override
    public void call(Object... objects) {
    System.out.println((JSONObject)objects[0]);
    }
    });

    socket.io().reconnection();
    }

    private JSONObject parseShortLecture(JSONObject obj) {
    JSONObject lec = null;
    try {
    lec = (JSONObject)obj.get("shortLecture");
    } catch (Exception ex) {
    System.out.println(ex.getMessage());
    }

    return lec;
    }

    private String parseOngoingId(JSONObject obj) {
    String res = null;
    JSONObject obj1 = parseShortLecture(obj);
    try {
    res = (String)obj1.get("ongoingId");
    } catch (JSONException ex) {
    System.out.println(ex.getMessage());
    }

    return res;
    }

    private String parseLectureState(JSONObject obj) {
    String res = null;
    try {
    res = (String)obj.get("type");
    } catch (JSONException ex) {
    System.out.println(ex.getMessage());
    }

    return res;
    }
    }