SignalR Java Client Implementation with Authentication
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you so much for the help