我在android端进行了Signal R集成,并完成了所有必要的步骤。并配置了服务器端代码。将服务器URL更改为我的localhost并运行应用程序,但它会抛出异常。
java.util.concurrent.ExecutionException: microsoft.aspnet.signalr.client.transport.NegotiationException: There was a problem in the negotiation with the server
。
以下是我的android端代码。请给我一个适当的指导。
*注意:我也根据服务器端配置更改了集线器名称。
public class SignalRService extends Service {
private microsoft.aspnet.signalr.client.hubs.HubConnection mHubConnection;
private microsoft.aspnet.signalr.client.hubs.HubProxy mHubProxy;
private Handler mHandler; // to display Toast message
private final IBinder mBinder = new LocalBinder(); // Binder given to clients
public static final String BROADCAST_ACTION = "com.android.com.simplechatwithsignalr";
public SignalRService() {
}
@Override
public void onCreate() {
super.onCreate();
mHandler = new Handler(Looper.getMainLooper());
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
int result = super.onStartCommand(intent, flags, startId);
startSignalR();
return result;
}
@Override
public void onDestroy() {
mHubConnection.stop();
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
// Return the communication channel to the service.
startSignalR();
return mBinder;
}
/**
* Class used for the client Binder. Because we know this service always
* runs in the same process as its clients, we don't need to deal with IPC.
*/
public class LocalBinder extends Binder {
public SignalRService getService() {
// Return this instance of SignalRService so clients can call public methods
return SignalRService.this;
}
}
/**
* method for clients (activities)
*/
public void sendMessage(String message) {
String SERVER_METHOD_SEND = "Send";
mHubProxy.invoke(SERVER_METHOD_SEND, "test" ,
message);
}
private void startSignalR() {
Platform.loadPlatformComponent(new AndroidPlatformComponent());
// String serverUrl = "http://192.168.0.104/";
String serverUrl = "http://10.1.81.65:5565/";
mHubConnection = new microsoft.aspnet.signalr.client.hubs.HubConnection(serverUrl);
String SERVER_HUB_CHAT = "Chat";
mHubProxy = mHubConnection.createHubProxy(SERVER_HUB_CHAT);
ClientTransport clientTransport = new ServerSentEventsTransport(mHubConnection.getLogger());
SignalRFuture<Void> signalRFuture = mHubConnection.start(clientTransport);
try {
signalRFuture.get();
} catch (InterruptedException | ExecutionException e) {
Log.e("SimpleSignalR", e.toString());
return;
}
mHubProxy.on("Send", new SubscriptionHandler1<String>() {
@Override
public void run(String msg) {
Log.e("onMessageReceived ", msg);
Intent intent = new Intent(BROADCAST_ACTION);
intent.putExtra("message", msg);
sendBroadcast(intent);
}
}, String.class);
mHubConnection.received(new MessageReceivedHandler() {
@Override
public void onMessageReceived(final JsonElement json) {
Log.e("onMessageReceived ", json.toString());
/* mHandler.post(new Runnable() {
@Override
public void run() {
Log.e("checkMessage" , "===-?" + json);
}
});*/
}
});
}
}