So I've been making a server for broadcast control recently however I just ran into some issues with the reading of the lines. When the connect comes in I am creating a new thread and that is where the connection is processed. All the data is supplied within the URL being called. For example:
/controllerupdate/?_=1597968275949
or
/api/
. These return different things from the mixer depending on the URL. The processing for everything else is minimal (<50ms on average).
However, the String line = br.readLine()
takes up to 700ms to call which is ages when broadcasting.
Here is the block of code:
@Override
public void run() {
String path = "";
try {
//BufferedReader br = new BufferedReader(new InputStreamReader(this.is));
BufferedReader in = new BufferedReader(new InputStreamReader(this.s.getInputStream()));
long timeIn = System.currentTimeMillis();
//String request = br.readLine(); // Now you get GET index.html HTTP/1.1
String line;
line = in.readLine();
long t = (System.currentTimeMillis() - timeIn);
String[] requestParam = line.split(" ");
path = requestParam[1];
if(t > 50) {
System.out.println("T1: " + path + " :: " + t);
}
//Rest of the processing code is here
} catch (Exception e) {
e.printStackTrace();
}
}
This code prints out the following:
Listening for connection on port 8080 ....
1597968951950 :: T1: /controllerupdate/?_=1597968950775 :: 187
1597968952013 :: T1: /api/ :: 766
1597968953450 :: T1: /controllerupdate/?_=1597968950778 :: 203
1597968953950 :: T1: /api/ :: 453
1597968954950 :: T1: /controllerupdate/?_=1597968950781 :: 203
1597968955950 :: T1: /api/ :: 703
1597968956450 :: T1: /controllerupdate/?_=1597968950784 :: 203
1597968956950 :: T1: /api/ :: 125
1597968957951 :: T1: /controllerupdate/?_=1597968950787 :: 204
1597968958951 :: T1: /api/ :: 345
1597968959450 :: T1: /controllerupdate/?_=1597968950790 :: 203
1597968960950 :: T1: /controllerupdate/?_=1597968950793 :: 203
1597968960950 :: T1: /api/ :: 453
The program that does the connecting is a C# program:
Here is the function that does the request and how it is called:
private async Task<String[]> doGetRequestWithError(String url)
{
if (!url.StartsWith("http"))
{
url = "http://" + url;
}
//Console.WriteLine(url);
try
{
HttpClient httpClient = new HttpClient();
HttpResponseMessage result = await httpClient.GetAsync(url).ConfigureAwait(false);
String error = result.StatusCode.ToString();
String content = await result.Content.ReadAsStringAsync();
return new String[] { content, error };
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace);
Console.WriteLine("ERROR: " + url);
}
return new string[] { "", "" };
}
Called by:
Task<String[]> tsk = this.doGetRequestWithError(this.server + "/mixer?id=" + this.id + "&function=next");
Althought the ones that are being slow in that log are from an Ajax call:
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var xml = this.responseXML;
var inputs = xml.getElementsByTagName("input");
var transitions = xml.getElementsByTagName("transition");
var numInputs = inputs.length;
var numTransition = Math.min(transitions.length, 4);
var names = [];
var types = [];
var isTitle = [];
var transNames = [];
inputsTmp = inputs;
for(var i = 0; i < numInputs; i++){
...
}
for(var i = 0; i < numTransition; i++){
...
}
...
$.getScript("http://" + server + "/controllerupdate/?refresh=1");
playbackUpdater();
window.setInterval(controllerUpdate, 500);
window.setInterval(playbackUpdater, 1000);
}
};
xhttp.open("GET", "http://" + server + "/api/", true);
xhttp.send();
So with this, I'm just wondering where I am going wrong? I've never had issues with this before!
Thank you in advance!