forked from j62/ctbrec
Properly end websocket download
End the websocket download if a model changes state from online. Also properly close both sockets and set isAlive to false, if the download is closed or fails
This commit is contained in:
parent
f2ac9e3657
commit
45df31eb15
|
@ -13,8 +13,13 @@ import org.json.JSONObject;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import com.google.common.eventbus.Subscribe;
|
||||||
|
|
||||||
import ctbrec.Config;
|
import ctbrec.Config;
|
||||||
import ctbrec.Model;
|
import ctbrec.Model;
|
||||||
|
import ctbrec.event.Event;
|
||||||
|
import ctbrec.event.EventBusHolder;
|
||||||
|
import ctbrec.event.ModelStateChangedEvent;
|
||||||
import ctbrec.io.HttpClient;
|
import ctbrec.io.HttpClient;
|
||||||
import ctbrec.recorder.download.Download;
|
import ctbrec.recorder.download.Download;
|
||||||
import okhttp3.Request;
|
import okhttp3.Request;
|
||||||
|
@ -67,6 +72,8 @@ public class LiveJasminWebSocketDownload implements Download {
|
||||||
LOG.debug("stream host: {}",streamHost);
|
LOG.debug("stream host: {}",streamHost);
|
||||||
LOG.debug("clientinstanceid {}",clientInstanceId);
|
LOG.debug("clientinstanceid {}",clientInstanceId);
|
||||||
|
|
||||||
|
EventBusHolder.BUS.register(this);
|
||||||
|
|
||||||
Request request = new Request.Builder()
|
Request request = new Request.Builder()
|
||||||
.url("https://" + relayHost + "/")
|
.url("https://" + relayHost + "/")
|
||||||
.header("Origin", "https://www.livejasmin.com")
|
.header("Origin", "https://www.livejasmin.com")
|
||||||
|
@ -98,13 +105,26 @@ public class LiveJasminWebSocketDownload implements Download {
|
||||||
sendToRelay("{\"event\":\"connectSharedObject\",\"name\":\"data/chat_so\"}");
|
sendToRelay("{\"event\":\"connectSharedObject\",\"name\":\"data/chat_so\"}");
|
||||||
}).start();
|
}).start();
|
||||||
} else if (event.optString("event").equals("updateSharedObject")) {
|
} else if (event.optString("event").equals("updateSharedObject")) {
|
||||||
// TODO
|
|
||||||
JSONArray list = event.getJSONArray("list");
|
JSONArray list = event.getJSONArray("list");
|
||||||
for (int i = 0; i < list.length(); i++) {
|
for (int i = 0; i < list.length(); i++) {
|
||||||
JSONObject obj = list.getJSONObject(i);
|
JSONObject obj = list.getJSONObject(i);
|
||||||
if (obj.optString("name").equals("streamList")) {
|
if (obj.optString("name").equals("streamList")) {
|
||||||
LOG.debug(obj.toString(2));
|
//LOG.debug(obj.toString(2));
|
||||||
streamPath = getStreamPath(obj.getJSONObject("newValue"));
|
streamPath = getStreamPath(obj.getJSONObject("newValue"));
|
||||||
|
} else if(obj.optString("name").equals("isPrivate")
|
||||||
|
|| obj.optString("name").equals("onPrivate")
|
||||||
|
|| obj.optString("name").equals("onPrivateAll")
|
||||||
|
|| obj.optString("name").equals("onPrivateLJ"))
|
||||||
|
{
|
||||||
|
if(obj.optBoolean("newValue")) {
|
||||||
|
// model went private, stop recording
|
||||||
|
LOG.debug("Model {} state changed to private -> stopping download", model.getName());
|
||||||
|
stop();
|
||||||
|
}
|
||||||
|
} else if(obj.optString("name").equals("recommendedBandwidth") || obj.optString("name").equals("realQualityData")) {
|
||||||
|
// stream quality related -> do nothing
|
||||||
|
} else {
|
||||||
|
LOG.debug("{} -{}", model.getName(), obj.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -120,13 +140,22 @@ public class LiveJasminWebSocketDownload implements Download {
|
||||||
}
|
}
|
||||||
}).start();
|
}).start();
|
||||||
}
|
}
|
||||||
}else if(event.optString("event").equals("call")) {
|
} else if(event.optString("event").equals("call")) {
|
||||||
String func = event.optString("funcName");
|
String func = event.optString("funcName");
|
||||||
if(func.equals("closeConnection")) {
|
if (func.equals("closeConnection")) {
|
||||||
connectionClosed = true;
|
connectionClosed = true;
|
||||||
//System.out.println(event.get("data"));
|
// System.out.println(event.get("data"));
|
||||||
stop();
|
stop();
|
||||||
|
} else if (func.equals("addLine")) {
|
||||||
|
// chat message -> ignore
|
||||||
|
} else if (func.equals("receiveInvitation")) {
|
||||||
|
// invitation to private show -> ignore
|
||||||
|
} else {
|
||||||
|
LOG.debug("{} -{}", model.getName(), event.toString());
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
if(!event.optString("event").equals("pong"))
|
||||||
|
LOG.debug("{} -{}", model.getName(), event.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -156,12 +185,14 @@ public class LiveJasminWebSocketDownload implements Download {
|
||||||
@Override
|
@Override
|
||||||
public void onClosed(WebSocket webSocket, int code, String reason) {
|
public void onClosed(WebSocket webSocket, int code, String reason) {
|
||||||
LOG.trace("relay closed {} {} {}", code, reason, model.getName());
|
LOG.trace("relay closed {} {} {}", code, reason, model.getName());
|
||||||
|
stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(WebSocket webSocket, Throwable t, Response response) {
|
public void onFailure(WebSocket webSocket, Throwable t, Response response) {
|
||||||
if(!connectionClosed) {
|
if(!connectionClosed) {
|
||||||
LOG.trace("relay failure {}", model.getName(), t);
|
LOG.trace("relay failure {}", model.getName(), t);
|
||||||
|
stop();
|
||||||
if (response != null) {
|
if (response != null) {
|
||||||
response.close();
|
response.close();
|
||||||
}
|
}
|
||||||
|
@ -170,6 +201,17 @@ public class LiveJasminWebSocketDownload implements Download {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Subscribe
|
||||||
|
public void handleEvent(Event evt) {
|
||||||
|
if(evt.getType() == Event.Type.MODEL_STATUS_CHANGED) {
|
||||||
|
ModelStateChangedEvent me = (ModelStateChangedEvent) evt;
|
||||||
|
if(me.getModel().equals(model) && me.getOldState() == Model.State.ONLINE) {
|
||||||
|
LOG.debug("Model {} state changed to {} -> stopping download", me.getNewState(), model.getName());
|
||||||
|
stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void sendToRelay(String msg) {
|
private void sendToRelay(String msg) {
|
||||||
LOG.trace("relay --> {} {}", model.getName(), msg);
|
LOG.trace("relay --> {} {}", model.getName(), msg);
|
||||||
relay.send(msg);
|
relay.send(msg);
|
||||||
|
@ -264,12 +306,14 @@ public class LiveJasminWebSocketDownload implements Download {
|
||||||
@Override
|
@Override
|
||||||
public void onClosed(WebSocket webSocket, int code, String reason) {
|
public void onClosed(WebSocket webSocket, int code, String reason) {
|
||||||
LOG.trace("stream closed {} {} {}", code, reason, model.getName());
|
LOG.trace("stream closed {} {} {}", code, reason, model.getName());
|
||||||
|
stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(WebSocket webSocket, Throwable t, Response response) {
|
public void onFailure(WebSocket webSocket, Throwable t, Response response) {
|
||||||
if(!connectionClosed) {
|
if(!connectionClosed) {
|
||||||
LOG.trace("stream failure {}", model.getName(), t);
|
LOG.trace("stream failure {}", model.getName(), t);
|
||||||
|
stop();
|
||||||
if (response != null) {
|
if (response != null) {
|
||||||
response.close();
|
response.close();
|
||||||
}
|
}
|
||||||
|
@ -281,9 +325,14 @@ public class LiveJasminWebSocketDownload implements Download {
|
||||||
@Override
|
@Override
|
||||||
public void stop() {
|
public void stop() {
|
||||||
connectionClosed = true;
|
connectionClosed = true;
|
||||||
stream.close(1000, "");
|
EventBusHolder.BUS.unregister(this);
|
||||||
relay.close(1000, "");
|
|
||||||
isAlive = false;
|
isAlive = false;
|
||||||
|
if (stream != null) {
|
||||||
|
stream.close(1000, "");
|
||||||
|
}
|
||||||
|
if (relay != null) {
|
||||||
|
relay.close(1000, "");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in New Issue