我在服务器端使用signalR
和signalR作为客户端(java)android方面
当我呼叫 SyncFromServer 时,一切正常,除了从服务器发送的每个响应,响应大约需要2分钟的时间(使用webservice仅为0.5 s),对于所有数据,甚至对于一个实体,总是2分钟。
contact是服务器中具有相同字段的类,并且所有获取的数据都是正确的。
这是我的android类来调用该函数:
public class SignalRService extends Service {
private HubConnection mHubConnection;
private HubProxy mHubProxy;
private Handler mHandler; // to display Toast message
private final IBinder mBinder = new LocalBinder(); // Binder given to clients
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 SignalRFuture<contact[]> SyncFromServer(int i) {
Log.e("call","SyncFromServer");
String SERVER_METHOD_GETAGENTS = "SyncFromServer";
SignalRFuture<contact[]> ls = mHubProxy.invoke(contact[].class,SERVER_METHOD_GETAGENTS,i);
Log.e("call","end SyncFromServer");
return ls;
}
private void startSignalR() {
Platform.loadPlatformComponent(new AndroidPlatformComponent());
Credentials credentials = new Credentials() {
@Override
public void prepareRequest(Request request) {
request.addHeader("User-Name", "BNK");
}
};
String serverUrl = "http://xx.xx.xx.xx/signalr";
mHubConnection = new HubConnection(serverUrl);
mHubConnection.setCredentials(credentials);
String SERVER_HUB = "AgentsHub";
mHubProxy = mHubConnection.createHubProxy(SERVER_HUB);
ClientTransport clientTransport = new ServerSentEventsTransport(mHubConnection.getLogger());
SignalRFuture<Void> signalRFuture = mHubConnection.start(clientTransport);
try {
signalRFuture.get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
return;
}
mHubConnection.error(new ErrorCallback() {
@Override
public void onError(Throwable error) {
Log.e("SignalR","Error"+error.getMessage());
error.printStackTrace(); //<==SocketException
}
});
}
}
服务器中心:
public class AgentsHub : Hub
{
agentsEntities1 db = new agentsEntities1();
public contact SyncFromServer(int i) {
var list =db.contact.ToList();
return list[i];
}
}
和启动类:
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.Map("/signalr", map =>
{
map.UseCors(CorsOptions.AllowAll);
var hubConfiguration = new HubConfiguration { };
hubConfiguration.EnableDetailedErrors = true;
map.RunSignalR(hubConfiguration);
});
}
}