Skip to content

Instantly share code, notes, and snippets.

@frankodoom
Last active Jul 31, 2019
Embed
What would you like to do?
SignalR Java Client Implementation with Authentication
@Override
public void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Signal R Configuration Begins
handler = new Handler((Looper.getMainLooper()));
Platform.loadPlatformComponent(new AndroidPlatformComponent());
String server = "SignalrUrl";
connection = new HubConnection(server);
mainHubProxy = connection.createHubProxy("BikerHub");
Credentials credentials = new Credentials() {
@Override
public void prepareRequest(Request request) {
request.addHeader("Authorization", mPrefManager.getAccessToken());
//request.addHeader("QCST", Otp.generateOtp());
}
};
connection.setCredentials(credentials);
connectSignalr();
mainHubProxy.subscribe("bikerJobsRequest").addReceivedHandler(new Action<JsonElement[]>() {
@Override
public void run(final JsonElement[] jsonElements) throws Exception {
handler.post(new Runnable() {
@Override
public void run() {
if (jsonElements[0].isJsonNull()) {
//Todo handle Json is Empty
}
//handle json object parsing
}
});
}
});
}
}
public void connectSignalr() {
try {
SignalRConnectTask signalRConnectTask = new SignalRConnectTask();
signalRConnectTask.execute(connection);
} catch (Exception ex) {
ex.printStackTrace();
}
}
// SignalR Async Task Connection
public class SignalRConnectTask extends AsyncTask {
@Override
protected Object doInBackground(Object[] objects) {
HubConnection connection = (HubConnection) objects[0];
try {
Thread.sleep(2000);
connection.start().get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
return null;
}
}
@ViktorMorgachev
Copy link

ViktorMorgachev commented Jul 31, 2019

Thank you so much for the help

@frankodoom
Copy link
Author

frankodoom commented Jul 31, 2019

you are welcome @ViktorMorgachev

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment