Add topic/subscription after stomp client is connected - android

I am currently using
https://github.com/NaikSoftware/StompProtocolAndroid
to connect websocket using STOMP. I have a simple implementation as
public class TestActivity extends AppCompatActivity {
private StompClient mStompCLient;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
ButterKnife.bind(this);
setSupportActionBar(toolbar);
mStompCLient = Stomp.over(WebSocket.class, BASE_URL);
mStompCLient.topic("/topic/online/" + mSharedPreferences.getPrivateKey()).subscribe(new Subscriber<StompMessage>() {
#Override
public void onCompleted() {
Log.i(TAG, "/topic/online/ onCompleted: ");
}
#Override
public void onError(Throwable e) {
Log.i(TAG, "/topic/online/ onError: " + e.getMessage());
}
#Override
public void onNext(StompMessage stompMessage) {
Log.d(TAG, "/topic/online/ onNext: " + stompMessage.getPayload());
String content = "";
JSONObject jsonResponse = null;
try {
jsonResponse = new JSONObject(stompMessage.getPayload());
content = jsonResponse.getString("uri");
} catch (JSONException e) {
e.printStackTrace();
}
listenToUpdatesFromFinalUri(content);
}
});
mStompCLient.lifecycle().subscribe(lifecycleEvent -> {
Log.i(TAG, "onCreate: " + lifecycleEvent.getMessage());
switch (lifecycleEvent.getType()) {
case OPENED:
Log.d(TAG, "Stomp connection opened");
break;
case ERROR:
Log.e(TAG, "Error", lifecycleEvent.getException());
break;
case CLOSED:
Log.d(TAG, "Stomp connection closed : " + lifecycleEvent.toString() + " :msg: " + lifecycleEvent.getMessage() + " :escep: " + lifecycleEvent.getException() + " :headers: " + lifecycleEvent.getHandshakeResponseHeaders() + " :type: " + lifecycleEvent.getType());
break;
}
});
mStompCLient.connect();
}
private void listenToUpdatesFromFinalUri(String content) {
mStompCLient.topic(content).subscribe(new Subscriber<StompMessage>() {
#Override
public void onCompleted() {
Log.i(TAG," onCompleted: ");
}
#Override
public void onError(Throwable e) {
Log.i(TAG, " onError: " + e.getMessage());
}
#Override
public void onNext(StompMessage stompMessage) {
Log.d(TAG, " onNext: " + stompMessage.getPayload());
}
});
}
#Override
protected void onStop() {
super.onStop();
disconnectStomp();
}
private void disconnectStomp() {
mStompCLient.disconnect();
}
}
Here I am trying to listen to the new subscription channel sent by the server after connection is established. It works if the subscribe() is called before the connect is called. But the final uri/subscription channel subscribed in listenToUpdatesFromFinalUri() function is not static so I need can't add subscription before the connect. I am currently unable to get response for the final uri/subscription. Any help is appreciated.

The issue has been solved in the new version 1.1.6

Related

SignalR negotiate method calling in loop sometimes in 24 hours

I have used below code to connect with hub.
Its working proper. but sometime suddenly the negotiate GET method calls thousands of time.
private HubConnection createLocalHubConnection() {
if (Common.isInternetAvailable(CallObserverTestService.this)) {
Log.i(TAG, "Create Connection");
final HubConnection loConnection = new HubConnection(WebService.getHubURL(CallObserverTestService.this));
Log.i(TAG, "Connection State1: " + loConnection.getState());
if (loConnection.getState() == ConnectionState.Disconnected) {
moProxy = loConnection.createHubProxy(CallObserverTestService.this.getString(R.string.hub));
try {
loConnection.stop();
loConnection.start().done(new Action<Void>() {
#Override
public void run(Void obj) throws Exception {
Log.i(TAG, "Connection URL: " + loConnection.getUrl());
Log.i(TAG, "SignalR ConnectionId: " + loConnection.getConnectionId());
}
}).onError(new ErrorCallback() {
#Override
public void onError(Throwable error) {
Log.i(TAG, "SignalR: onError : " + error.getMessage());
error.printStackTrace();
Log.logException(TAG, error);
}
});
} catch (Exception e) {
e.printStackTrace();
Log.logException(TAG, e);
}
}
return loConnection;
}
return null;
}
The error is as below while calling in loop
Negotiation error: There was a problem in the negotiation with the
server
negotiate: https://MyDomain/signalr/negotiate?clientProtocol=1.3&connectionData=%5B%7B%22name%22%3A%22MyHubName%22%7D%5D
Reference link: https://github.com/SignalR/java-client/tree/master/signalr-client-sdk

InMobi SDK 7 Android | InMobi needs to be initialized before calling Interstitial Request

I have integrated InMobi 7.2.9 in my App and initialize it like this:
JSONObject consent = new JSONObject();
try {
consent.put(InMobiSdk.IM_GDPR_CONSENT_AVAILABLE, true);
} catch (JSONException e) {
Log.d(TAG, "Init InMobiSDK failed");
e.printStackTrace();
}
InMobiSdk.init(cordova.getActivity(), accountID, consent);
InMobiSdk.setLogLevel(InMobiSdk.LogLevel.DEBUG);
After that I initialize my Interstitial like this:
mInterstitialAd = new InMobiInterstitial(cordova.getActivity(), placementId,
new InterstitialAdEventListener() {
#Override
public void onAdLoadSucceeded(InMobiInterstitial inMobiInterstitial) {
super.onAdLoadSucceeded(inMobiInterstitial);
Log.d(TAG, "onAdLoadSuccessful");
if (!inMobiInterstitial.isReady()) {
Log.d(TAG, "onAdLoadSuccessful inMobiInterstitial not ready");
}
}
#Override
public void onAdLoadFailed(InMobiInterstitial inMobiInterstitial, InMobiAdRequestStatus inMobiAdRequestStatus) {
super.onAdLoadFailed(inMobiInterstitial, inMobiAdRequestStatus);
Log.d(TAG, "Unable to load interstitial ad (error message: " + inMobiAdRequestStatus.getMessage());
}
#Override
public void onAdReceived(InMobiInterstitial inMobiInterstitial) {
super.onAdReceived(inMobiInterstitial);
Log.d(TAG, "onAdReceived");
}
#Override
public void onAdClicked(InMobiInterstitial inMobiInterstitial, Map<Object, Object> map) {
super.onAdClicked(inMobiInterstitial, map);
Log.d(TAG, "onAdClicked " + map.size());
}
#Override
public void onAdWillDisplay(InMobiInterstitial inMobiInterstitial) {
super.onAdWillDisplay(inMobiInterstitial);
Log.d(TAG, "onAdWillDisplay " + inMobiInterstitial);
}
#Override
public void onAdDisplayed(InMobiInterstitial inMobiInterstitial) {
super.onAdDisplayed(inMobiInterstitial);
Log.d(TAG, "onAdDisplayed " + inMobiInterstitial);
}
#Override
public void onAdDisplayFailed(InMobiInterstitial inMobiInterstitial) {
super.onAdDisplayFailed(inMobiInterstitial);
Log.d(TAG, "onAdDisplayFailed " + "FAILED");
}
#Override
public void onAdDismissed(InMobiInterstitial inMobiInterstitial) {
super.onAdDismissed(inMobiInterstitial);
Log.d(TAG, "onAdDismissed " + inMobiInterstitial);
}
#Override
public void onUserLeftApplication(InMobiInterstitial inMobiInterstitial) {
super.onUserLeftApplication(inMobiInterstitial);
Log.d(TAG, "onUserWillLeaveApplication " + inMobiInterstitial);
}
#Override
public void onRewardsUnlocked(InMobiInterstitial inMobiInterstitial, Map<Object, Object> map) {
super.onRewardsUnlocked(inMobiInterstitial, map);
Log.d(TAG, "onRewardsUnlocked " + map.size());
}
});
Log.d(TAG, "Start to load Interstitial");
mInterstitialAd.load();
To init the InMobi SDK I use the accountID from my InMobi Account and placementID for my Interstitial Ad.First I init my InMobi and after that I load the Interstitial. But somehow Im always getting the Erro:
You need to initialize InMobi first before calling Interstitial Ads. Call ignored.
I used this sample code from inmobi but somehow InmobiSdk.init doesnt work.

pubnub duplicate messages android

I was hoping someone may be able to help or at least point me in the right direction. I'm writing an android app that publishes/receives messages and i'm trying to use PubNub to accomplish this task. In my view I firstly unsubscribe from a group name and then subscribe to it. When I then publish a message, I successfully get one message. If I then leave the view and re-enter it, when I publish a message I get a duplicate. If I do the same again, the message is triplicated and so on.
Could someone please look at my code or give me any advice.
Many thanks.
private Pubnub pubnub = new Pubnub("pub-key", "sub-key");
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(com.rickeshjobanputra.snapapp.R.layout.activity_group);
GroupName = intent.getStringExtra("GroupName");
String uuid = "12345";
pubnub.setUUID(uuid);
pubnub.unsubscribe(GroupName);
try {
pubnub.subscribe(GroupName, new Callback() {
#Override
public void successCallback(String channel, final Object message) {
System.out.println("SUBSCRIBE : " + channel + " : "
+ message.getClass() + " : " + message.toString());
}
});
} catch (PubnubException e) {
System.out.println(e.toString());
}
addListenerOnButton();
}
private void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(com.rickeshjobanputra.snapapp.R.id.snap_something_button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
pubnub.publish(GroupName, "test", new Callback() {
});
}
});
}
UPDATE
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
pubnub.unsubscribeAllChannels();
try {
pubnub.subscribe("test", new Callback() {
#Override
public void connectCallback(String channel, Object message) {
pubnub.publish("test", "Hello from the PubNub Java SDK", new Callback() {});
}
#Override
public void successCallback(String channel, final Object message) {
System.out.println("SUBSCRIBE : " + channel + " : "
+ message.getClass() + " : " + message.toString());
}
});
} catch (PubnubException e) {
System.out.println(e.toString());
}
}

Using Autobahn for WebSocket comms, not receiving from Server

I'm implement Autobahn to connnect to a server through WebSockets. When I hit connect, it opens the socket correctly and logs that socket is opened. I then try to send a request to the server which is simply {"request":"getSoftwareVersion"} , when the server receives this, it should send back the software version in a JSON object, the trouble is, that onMessage is never hit. Here is my code:
public class AutoBahnConnectRequest extends Request{
private static WebSocketConnection mAutoBahnConnection;
private String mSocketHostAddress;
private final static String m_TAG = AutoBahnConnectRequest.class.getSimpleName();
public AutoBahnConnectRequest(String SocketHostAddress){
this.mAutoBahnConnection = new WebSocketConnection();
this.mSocketHostAddress = SocketHostAddress;
}
#Override
protected Void doInBackground(Void... params){
try {
mAutoBahnConnection.connect(mSocketHostAddress, new WebSocketHandler(){
#Override
public void onOpen() {
String requestSoftware = "{\"request\":\"getSoftwareVersion\"}";
Log.i(m_TAG, requestSoftware);
Log.i(m_TAG, "Status: Connected to " + mSocketHostAddress);
mAutoBahnConnection.sendTextMessage(requestSoftware);
}
#Override
public void onTextMessage(String payload) {
Log.i(m_TAG, "Got echo: " + payload);
}
#Override
public void onClose(int code, String reason) {
Log.i(m_TAG, "Connection lost."+ reason);
}
});
} catch (WebSocketException e) {
Log.d(m_TAG, e.toString());
}
return null;
}
}
This has been implemented with a html client like so (not with autobahn):
function getSoftwareVersion() {
socket_di.send('{"request":"getSoftwareVersion"}');
}
and the onMessage receives the data. Can someone please tell me if I'm doing something wrong here?
Thank you.
I have figured out my issue regarding this. The WebSocket connection required a protocol and options to be added. So I changed this:
mAutoBahnConnection.connect(mSocketHostAddress, new WebSocketHandler(){
#Override
public void onOpen() {
String requestSoftware = "{\"request\":\"getSoftwareVersion\"}";
Log.i(m_TAG, requestSoftware);
Log.i(m_TAG, "Status: Connected to " + mSocketHostAddress);
mAutoBahnConnection.sendTextMessage(requestSoftware);
}
#Override
public void onTextMessage(String payload) {
Log.i(m_TAG, "Got echo: " + payload);
}
#Override
public void onClose(int code, String reason) {
Log.i(m_TAG, "Connection lost."+ reason);
}
});
} catch (WebSocketException e) {
Log.d(m_TAG, e.toString());
}
to this:
mAutoBahnConnection.connect(mSocketHostAddress,new String[]{"this is my protocol"} ,new WebSocketHandler(){
#Override
public void onOpen() {
String requestSoftware = "{\"request\":\"getSoftwareVersion\"}";
Log.i(m_TAG, requestSoftware);
Log.i(m_TAG, "Status: Connected to " + mSocketHostAddress);
mAutoBahnConnection.sendTextMessage(requestSoftware);
}
#Override
public void onTextMessage(String payload) {
Log.i(m_TAG, "Got echo: " + payload);
}
#Override
public void onRawTextMessage(byte[] payload) {
try {
rawText = new String(payload, "UTF-8");
Log.i(m_TAG, "ON RAW TEXT");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
#Override
public void onBinaryMessage(byte[] payload) {
Log.i(m_TAG, "ON BINARY MESSAGE");
}
#Override
public void onClose(int code, String reason) {
Log.i(m_TAG, "Connection lost."+ reason);
}
}, options);
} catch (WebSocketException e) {
Log.d(m_TAG, e.toString());
}

Android - Autobahn Websocket sending message error(NullPointerException)

I am working on websocket communication with Autobahn.
On the main.class of my app, I set to call 'connect()' when users click a button.
// Toggle Button event
tButton = (ToggleButton) findViewById(R.id.toggleButton1);
tButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if(isChecked){
}else{
}
}
});
And after that, there is MyOffers.class, and if this class is accessed,
MyOffers_Fragment class is produced four times automatically, because MyOffers.class contains 'carousel view' and there are four products.
On 'MyOffers_Fragment' class, when users click one of the image of products, message should be sent.
if (pos == 0) {
product_photo.setImageResource(R.drawable.myoffers_0);
product_photo.setOnClickListener(new ImageButton.OnClickListener(){
public void onClick(View v){
String id = "Product0";
Log.d(TAG, "Current product is : " + id);
A.sendMessage(id);
}
});
}
But 'mConnection.sendTextMessage(id1);' this line makes 'NullPointerException' error.
There is a class 'Websocket_Connector.class'
public class WebSocket_Connector {
private static final String TAG = "ECHOCLIENT";
public final WebSocketConnection mConnection = new WebSocketConnection();
public void connect(final String wsuri) {
Log.d(TAG, "Connecting to: " + wsuri);
try {
mConnection.connect(wsuri, new WebSocketHandler() {
#Override
public void onOpen() {
Log.d(TAG, "Status: Connected to " + wsuri );
Log.d(TAG, "Connection successful!\n");
}
#Override
public void onTextMessage(String payload) {
Log.d(TAG, "Got echo: " + payload);
}
#Override
public void onClose(int code, String reason) {
Log.d(TAG, "Connection closed.");
}
});
} catch (WebSocketException e) {
Log.d(TAG, e.toString());
}
public void sendMessage(String message) {
connect("ws://192.168.x.xxx:xxxx");
mConnection.sendTextMessage(message);
}
}
I called 'connect()' in the main page class, and after that try to send message.
But, it's not working..Can you please help me out?
public class WebSocket_Connector {
private static final String TAG = "ECHOCLIENT";
public final WebSocketConnection mConnection = new WebSocketConnection();
private String tmpString = "";
public void connect(final String wsuri) {
Log.d(TAG, "Connecting to: " + wsuri);
try {
mConnection.connect(wsuri, new WebSocketHandler() {
#Override
public void onOpen() {
Log.d(TAG, "Status: Connected to " + wsuri );
Log.d(TAG, "Connection successful!\n");
mConnection.sendTextMessage(tmpString);
tmpString = "";
}
#Override
public void onTextMessage(String payload) {
Log.d(TAG, "Got echo: " + payload);
}
#Override
public void onClose(int code, String reason) {
Log.d(TAG, "Connection closed.");
}
});
} catch (WebSocketException e) {
Log.d(TAG, e.toString());
}
public void sendMessage(String message) {
if (mConnection.isConnected())
mConnection.sendTextMessage(message);
else {
tmpString = message;
connect("ws://192.168.x.xxx:xxxx");
}
}
}

Categories

Resources