SignalR的Android客户端没有收到,但JS没有收到
作者:互联网
这是一个“hello world”应用程序,但我找不到解决方案.
这是客户端上的代码:
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);
}
}
我认为错误在于此代码,因此我不附加服务器和JS客户端的代码.
解决方法:
您可以参考我的以下示例代码.我测试过了.希望这可以帮助!
public class MainActivity extends AppCompatActivity {
private TextView mTextView;
private final Context mContext = this;
private Handler mHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.textView);
mHandler = new Handler(Looper.getMainLooper());
Platform.loadPlatformComponent(new AndroidPlatformComponent());
Credentials credentials = new Credentials() {
@Override
public void prepareRequest(Request request) {
request.addHeader("User-Name", "BNK");
}
};
String serverUrl = "http://192.168.1.100/signalr";
HubConnection connection = new HubConnection(serverUrl);
connection.setCredentials(credentials);
HubProxy hubProxy = connection.createHubProxy("ChatHub");
ClientTransport clientTransport = new ServerSentEventsTransport(connection.getLogger());
SignalRFuture<Void> awaitConnection = connection.start(clientTransport);
try {
awaitConnection.get();
} catch (InterruptedException | ExecutionException e) {
Log.e("SignalR", e.toString());
e.printStackTrace();
return;
}
hubProxy.on("SimpleMessage", new SubscriptionHandler1<String>() {
@Override
public void run(final String s) {
Log.i("SignalR", s);
mHandler.post(new Runnable() {
@Override
public void run() {
mTextView.setText(mTextView.getText() + "\n" + s);
Toast.makeText(mContext, "message recieved: " + s, Toast.LENGTH_LONG).show();
}
});
}
}, String.class);
}
...
}
标签:android,signalr,signalr-client 来源: https://codeday.me/bug/20190628/1315028.html