This is a "hello world" app but I can't find the solution.
This is the code on the client:
public class MainActivity extends ActionBarActivity {
HubProxy proxy;
EditText messageText;
TextView resultText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Platform.loadPlatformComponent(new AndroidPlatformComponent());
//Setup connection
String server = "http://10.0.2.2:8081/";
HubConnection connection = new HubConnection(server);
proxy = connection.createHubProxy("chatHub");
Toast.makeText(getApplicationContext(),proxy.toString(),Toast.LENGTH_SHORT).show();
resultText = (TextView) findViewById(R.id.resultText);
//Start connection
SignalRFuture<Void> awaitConnection = connection.start();
//--HERE IS WHERE I AM NOT SURE HOW TO SET IT UP TO RECEIVE A NOTIFICATION---
//Setup to receive broadcast message
proxy.on("SimpleMessage", new SubscriptionHandler1<String>() {
@Override
public void run(String s) {
resultText.setText(resultText.getText()+"\n"+s.toString());
Toast.makeText(getApplicationContext(), "message recieved: "+s.toString(), Toast.LENGTH_LONG).show();
}
}
, String.class);
proxy.on("Alert", new SubscriptionHandler() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "Alert Recieved!!!", Toast.LENGTH_LONG).show();
//resultText.setText(resultText.getText()+"\nAlert Recieved!!!");
}
});
//---------------------------------------------------------------------------
proxy.subscribe(this);
try {
awaitConnection.get();
Toast.makeText(getApplicationContext(), "success", Toast.LENGTH_LONG).show();
} catch (InterruptedException e) {
Toast.makeText(getApplicationContext(), "un success", Toast.LENGTH_LONG).show();
} catch (ExecutionException e) {
Toast.makeText(getApplicationContext(), "in success: " + e.getMessage().toString(), Toast.LENGTH_LONG).show();
}
messageText = (EditText) findViewById(R.id.messageText);
Button sendBTN = (Button) findViewById(R.id.sendBTN);
sendBTN.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
String textToSend = messageText.getText().toString();
proxy.invoke("Send", "Android", textToSend ).get();
} catch (InterruptedException e) {
// Handle ...
} catch (ExecutionException e) {
// Handle ...
}
}
});
}
public void Alert(){
Toast.makeText(getApplicationContext(), "Alert Recieved!!!", Toast.LENGTH_LONG).show();
//resultText.setText(resultText.getText()+"\nAlert Recieved!!!");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I think the mistake is in this code and therefore I do not append the code of the server and the JS client.
You can refer to my following sample code. I have tested. Hope this helps!