Skip to content

Instantly share code, notes, and snippets.

@ilev4ik
Created March 10, 2017 19:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ilev4ik/bc3614581f771245398d4a50bd0d0359 to your computer and use it in GitHub Desktop.
Save ilev4ik/bc3614581f771245398d4a50bd0d0359 to your computer and use it in GitHub Desktop.
socket.io-sync.visualmath.ru
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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment