我正在开发和聊天应用程序,因此我使用的是SignalR Android SDK - Link Here。
我能够连接到服务器。但是我无法调用集线器方法 GetIncomingChatQueue 并接收它的响应。
服务
public class SignalRService extends Service {
private HubConnection mHubConnection;
private HubProxy mHubProxy;
private Handler mHandler; // to display Toast message
private final LocalBinder mBinder = new LocalBinder();
public SignalRService() {
}
@Override
public void onCreate() {
super.onCreate();
Log.d("Broadcast service", "Inside oncreate - service");
mHandler = new Handler(Looper.getMainLooper());
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("Broadcast service", "service start - service");
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.
Log.d("Broadcast service", "onBind - service");
startSignalR();
return (IBinder) 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) {
SharedPreferences prefs = getSharedPreferences("zupportdesk", MODE_PRIVATE);
String ProfileId = prefs.getString("ProfileId", "Not defined");
String profileToken = prefs.getString("profileToken", "Not defined");
Log.d("Broadcast service", "Inside send message - service");
//String SERVER_METHOD_SEND = "checkWebServiceStatus";
String SERVER_METHOD_SEND = "GetIncomingChatQueue("+ProfileId+","+profileToken+")";
//mHubProxy.invoke(SERVER_METHOD_SEND, message);
mHubProxy.invoke(SERVER_METHOD_SEND, message);
}
private void startSignalR() {
// Create a new console logger
Logger logger = new Logger() {
@Override
public void log(String message, LogLevel level) {
Log.d("Log Message : ", message);
}
};
// Connect to the server
HubConnection conn = new HubConnection("https://MY DOMAIN HERE/", "", true, logger);
// Create the hub proxy
HubProxy proxy = conn.createHubProxy("chatHub");
proxy.subscribe(new Object() {
@SuppressWarnings("unused")
public void messageReceived(String name, String message) {
Log.d("Log Message : ", name + ": " + message);
}
});
// Subscribe to the error event
conn.error(new ErrorCallback() {
@Override
public void onError(Throwable error) {
error.printStackTrace();
}
});
// Subscribe to the connected event
conn.connected(new Runnable() {
@Override
public void run() {
System.out.println("CONNECTED");
}
});
// Subscribe to the closed event
conn.closed(new Runnable() {
@Override
public void run() {
System.out.println("DISCONNECTED");
}
});
// Start the connection
conn.start()
.done(new Action<Void>() {
@Override
public void run(Void obj) throws Exception {
System.out.println("Done Connecting!");
}
});
// Subscribe to the received event
conn.received(new MessageReceivedHandler() {
@Override
public void onMessageReceived(JsonElement json) {
System.out.println("RAW received message: " + json.toString());
}
});
}
活动:
class Chats extends NavigationLiveo implements OnItemClickListener {
private final Context mContext = this;
private SignalRService mService;
private boolean mBound = false;
private HelpLiveo mHelpLiveo;
@Override
public void onInt(Bundle savedInstanceState) {
// User Information
this.userName.setText("K.sathyabaman");
this.userEmail.setText("sathya@rfgdgfdfgffdg.com");
this.userPhoto.setImageResource(R.drawable.baman);
this.userBackground.setImageResource(R.drawable.ic_user_background_first);
// Creating items navigation
mHelpLiveo = new HelpLiveo();
mHelpLiveo.add(getString(R.string.chats), R.drawable.ic_inbox_black_24dp, 7);
mHelpLiveo.addSeparator(); //Item separator
mHelpLiveo.addSubHeader(getString(R.string.sub_menu)); //Item subHeader
mHelpLiveo.add(getString(R.string.visitors), R.drawable.ic_star_black_24dp);
mHelpLiveo.add(getString(R.string.operators), R.drawable.ic_send_black_24dp);
mHelpLiveo.addNoCheck(getString(R.string.transcripts), R.drawable.ic_drafts_black_24dp);
mHelpLiveo.addNoCheck(getString(R.string.canned_responses), R.drawable.ic_drafts_black_24dp);
mHelpLiveo.addNoCheck(getString(R.string.feedback), R.drawable.ic_drafts_black_24dp);
mHelpLiveo.addNoCheck(getString(R.string.aboutUs), R.drawable.ic_drafts_black_24dp);
mHelpLiveo.addSeparator(); //Item separator
//{optional} - Header Customization - method customHeader
//View mCustomHeader = getLayoutInflater().inflate(R.layout.custom_header_user, this.getListView(), false);
//ImageView imageView = (ImageView) mCustomHeader.findViewById(R.id.imageView);
with(this).startingPosition(2) //Starting position in the list
.addAllHelpItem(mHelpLiveo.getHelp())
//{optional} - List Customization "If you remove these methods and the list will take his white standard color"
//.selectorCheck(R.drawable.selector_check) //Inform the background of the selected item color
//.colorItemDefault(R.color.nliveo_blue_colorPrimary) //Inform the standard color name, icon and counter
.colorItemSelected(R.color.nliveo_blue_colorPrimary) //State the name of the color, icon and meter when it is selected
//.colorLineSeparator(R.color.nliveo_transparent) //Inform the color of the subheader line
//{optional} - SubHeader Customization
//.colorNameSubHeader(R.color.nliveo_blue_colorPrimary)
//.colorLineSeparator(R.color.nliveo_green_colorPrimaryDark)
//.removeFooter()
.footerItem(R.string.settings, R.drawable.ic_settings_black_24dp)
//{optional} - Second footer
//.footerSecondItem(R.string.settings, R.drawable.ic_settings_black_24dp)
//{optional} - Header Customization
//.customHeader(mCustomHeader)
//{optional} - Footer Customization
//.footerNameColor(R.color.nliveo_blue_colorPrimary)
//.footerIconColor(R.color.nliveo_blue_colorPrimary)
//.footerSecondNameColor(R.color.nliveo_blue_colorPrimary)
//.footerSecondIconColor(R.color.nliveo_blue_colorPrimary)
//.footerBackground(R.color.nliveo_white)
//{optional} - Remove color filter icon
//.removeColorFilter()
.setOnClickUser(onClickPhoto)
.setOnPrepareOptionsMenu(onPrepare)
.setOnClickFooter(onClickFooter)
//{optional} - Second footer
//.setOnClickFooterSecond(onClickFooter)
.build();
int position = this.getCurrentPosition();
this.setElevationToolBar(position != 2 ? 15 : 0);
Intent intent = new Intent();
intent.setClass(mContext, SignalRService.class);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
sendMessage();
}
@Override
protected void onStop() {
// Unbind from the service
if (mBound) {
unbindService(mConnection);
mBound = false;
}
super.onStop();
}
public void sendMessage() {
Log.d("Main Activity : ", "Inside sendMessage start - Activity ");
if (mBound) {
// Call a method from the SignalRService.
// However, if this call were something that might hang, then this request should
// occur in a separate thread to avoid slowing down the activity performance.
String message = "hi sathya , This is a message";
mService.sendMessage(message);
}
}
/**
* Defines callbacks for service binding, passed to bindService()
*/
private final ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className,
IBinder service) {
Log.d("Main Activity : ", "Inside service connected - Activity ");
// We've bound to SignalRService, cast the IBinder and get SignalRService instance
SignalRService.LocalBinder binder = (SignalRService.LocalBinder) service;
mService = binder.getService();
mBound = true;
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
mBound = false;
}
};
@Override
public void onItemClick(int position) {
Fragment mFragment;
FragmentManager mFragmentManager = getSupportFragmentManager();
switch (position){
case 2:
mFragment = new ViewPagerFragment();
break;
default:
mFragment = MainFragment.newInstance(mHelpLiveo.get(position).getName());
break;
}
if (mFragment != null){
mFragmentManager.beginTransaction().replace(R.id.container, mFragment).commit();
}
setElevationToolBar(position != 2 ? 15 : 0);
}
private OnPrepareOptionsMenuLiveo onPrepare = new OnPrepareOptionsMenuLiveo() {
@Override
public void onPrepareOptionsMenu(Menu menu, int position, boolean visible) {
}
};
private View.OnClickListener onClickPhoto = new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "onClickPhoto :D", Toast.LENGTH_SHORT).show();
closeDrawer();
}
};
private View.OnClickListener onClickFooter = new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(), Settings.class));
closeDrawer();
}
};
我得到的回复:
08-23 12:28:08.492 8573-8782/baman.lankahomes.lk.zupportdeskchatsystem D/Log Message :: serverSentEvents - Read the response content by line
08-23 12:28:08.492 8573-8782/baman.lankahomes.lk.zupportdeskchatsystem D/Log Message :: serverSentEvents - Found new data: data: initialized
08-23 12:28:08.492 8573-8782/baman.lankahomes.lk.zupportdeskchatsystem D/Log Message :: serverSentEvents - Initialization message found
08-23 12:28:08.500 8573-8782/baman.lankahomes.lk.zupportdeskchatsystem D/Log Message :: serverSentEvents - Found new data: data: {"C":"s-0,5482","S":1,"M":[]}
08-23 12:28:08.500 8573-8782/baman.lankahomes.lk.zupportdeskchatsystem D/Log Message :: serverSentEvents - Trigger onData: {"C":"s-0,5482","S":1,"M":[]}
08-23 12:28:08.500 8573-8782/baman.lankahomes.lk.zupportdeskchatsystem D/Log Message :: HubConnection - Received data:
08-23 12:28:08.500 8573-8782/baman.lankahomes.lk.zupportdeskchatsystem D/Log Message :: MessageId received: s-0,5482
08-23 12:28:08.500 8573-8782/baman.lankahomes.lk.zupportdeskchatsystem D/Log Message :: Initialization message received
08-23 12:28:16.471 8573-8782/baman.lankahomes.lk.zupportdeskchatsystem D/Log Message :: serverSentEvents - Found new data: data: {}
08-23 12:28:16.471 8573-8782/baman.lankahomes.lk.zupportdeskchatsystem D/Log Message :: serverSentEvents - Trigger onData: {}
08-23 12:28:16.471 8573-8782/baman.lankahomes.lk.zupportdeskchatsystem D/Log Message :: HubConnection - Received data:
08-23 12:28:26.495 8573-8782/baman.lankahomes.lk.zupportdeskchatsystem D/Log Message :: serverSentEvents - Found new data: data: {}
08-23 12:28:26.495 8573-8782/baman.lankahomes.lk.zupportdeskchatsystem D/Log Message :: serverSentEvents - Trigger onData: {}
08-23 12:28:26.495 8573-8782/baman.lankahomes.lk.zupportdeskchatsystem D/Log Message :: HubConnection - Received data:
08-23 12:28:36.525 8573-8782/baman.lankahomes.lk.zupportdeskchatsystem D/Log Message :: serverSentEvents - Found new data: data: {}
有人可以帮助我调用特定的集线器方法并从集线器获得响应。