我在android应用中使用的是signalr,当我使用的数据不是wifi(wifi似乎工作正常)时,信号器会在几分钟后请求死亡。
btnRefresh似乎显示为红色,但是当您向下滚动到以后的消息时,即使有其他消息,btnRefresh也始终保持黑色。
sendMessage似乎每次都会触发,并且没有连接丢失,因为我那里有断点。
这是我的hubproxy,当我启动信号器时:
public class ClientChatHub extends Service {
private HubConnection mHubConnection;
private HubProxy mHubProxy;
private Handler mHandler; // to display Toast message
private final IBinder mBinder = new LocalBinder();
Button btnRefresh;
public ClientChatHub() {
}
@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;
}
Activity act;
/**
* 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 ClientChatHub getService() {
// Return this instance of SignalRService so clients can call public methods
return ClientChatHub.this;
}
}
/**
* method for clients (activities)
*/
public void sendMessage(int id, String searchThreadTitle, String searchThreadDesc, int actCatsearchVal, Button btnRefresh) {
String SERVER_METHOD_SEND = "Send";
this.btnRefresh = btnRefresh;
mHubProxy.invoke(SERVER_METHOD_SEND, id, searchThreadTitle, searchThreadDesc, actCatsearchVal);
//this.act = act;
}
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://xxxx/";
mHubConnection = new HubConnection(serverUrl);
//mHubConnection.setCredentials(credentials);
String SERVER_HUB_CHAT = "Notifications";
mHubProxy = mHubConnection.createHubProxy(SERVER_HUB_CHAT);
ClientTransport transport = new LongPollingTransport(mHubConnection.getLogger());
SignalRFuture<Void> signalRFuture = mHubConnection.start(transport);
try {
signalRFuture.get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
return;
}
try{
String CLIENT_METHOD_BROADAST_MESSAGE = "messageReceived";
mHubProxy.on(CLIENT_METHOD_BROADAST_MESSAGE,
new SubscriptionHandler1<Object>() {
@Override
public void run(final Object msg) {
final boolean finalMsg = (boolean) msg;
// display Toast message
mHandler.post(new Runnable() {
@Override
public void run() {
if(finalMsg){
if(btnRefresh != null) {
btnRefresh.setTextColor(Color.parseColor("#D32F2F"));
}
}else{
if(btnRefresh != null) {
btnRefresh.setTextColor(Color.parseColor("#000000"));
}
}
}
});
}
}
, Object.class);
}catch(Exception e){
String t = "";
}
}
}
这是startup.cs:
using Microsoft.Owin;
using Owin;
using Microsoft.Owin.Cors;
using Microsoft.AspNet.SignalR;
[assembly: OwinStartupAttribute(typeof(ThreadBumpApp.Startup))]
namespace xxxxx
{
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseCors(CorsOptions.AllowAll);
app.MapSignalR();
}
}
}
chathub脚本:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;
namespace xxxx.ChatHub
{
public class Notifications : Hub
{
public void Send(int id, string searchThreadTitle, string searchThreadDesc, int actCatsearchVal)
{
Boolean isNewThread = false;
if (id > 0)
{
isNewThread = new xxx.BAL.Data().isNewThread(id, searchThreadTitle, searchThreadDesc, actCatsearchVal);
}
Clients.AllExcept(Context.ConnectionId).messageReceived(isNewThread);
}
}
}
但是HubProxy.on似乎在几分钟后只能在Data不在wifi上停止工作。
有人可以帮我解决这个问题吗?。
谢谢。