Set Up A Stomp Client in Android With Spring Framework in Server Side Stack Overflow PDF
Set Up A Stomp Client in Android With Spring Framework in Server Side Stack Overflow PDF
x Dismiss
Sign up
@Configuration BLOG
//@EnableScheduling
4
@ComponentScan( Stack Overflow Podcast #100 - Jeff
basePackages="project.web", Atwood Is Back! (For Today)
excludeFilters = @ComponentScan.Filter(type= FilterType.ANNOTATION, value =
Configuration.class) Developers without Borders: The
) Global Stack Overflow Network
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/message");
http://stackoverflow.com/questions/24346068/set-up-a-stomp-client-in-android-with-spring-framework-in-server-side[01/02/2017 00:56:53]
Set up a Stomp client in android with Spring framework in server side - Stack Overflow
config.setApplicationDestinationPrefixes("/app"); Get the weekly newsletter! In it, you'll get:
}
The week's top questions and answers
@Override Important community announcements
public void registerStompEndpoints(StompEndpointRegistry registry) { Questions that need answers
registry.addEndpoint("/client");
}
} Sign up for the newsletter
and a SimpMessageSendingOperations in Spring controller to send message from server to client : see an example newsletter
@Controller
public class MessageAddController { Linked
private final Log log = LogFactory.getLog(MessageAddController.class);
0
private SimpMessageSendingOperations messagingTemplate;
How connect android device to Spring websocket
private UserManager userManager; server
http://stackoverflow.com/questions/24346068/set-up-a-stomp-client-in-android-with-spring-framework-in-server-side[01/02/2017 00:56:53]
Set up a Stomp client in android with Spring framework in server side - Stack Overflow
web browser test : Spring stomp over websocket client reconnects
causes a huge open file descriptors
var stompClient = null;
0
function setConnected(connected) { Stomp SockJs client + server Spring
document.getElementById('connect').disabled = connected;
document.getElementById('disconnect').disabled = !connected;
document.getElementById('conversationDiv').style.visibility = connected ?
Hot Network Questions
'visible' : 'hidden';
Why did Fernand stop escaping after he shot
document.getElementById('response').innerHTML = '';
Mercedes?
}
Drawbacks of participating in a conference boycott
function connect() {
stompClient = Stomp.client("ws://YOUR_IP/client"); How could immortal children age faster than
stompClient.connect({}, function(frame) { immortal adults?
setConnected(true); When does a player have to state they are
stompClient.subscribe('/message/add', function(message){ making a passive check?
showMessage(JSON.parse(message.body).content);
}); Should a player know their mount's exact HP?
});
more hot questions
}
function disconnect() {
stompClient.disconnect();
setConnected(false);
console.log("Disconnected");
}
function showMessage(message) {
var response = document.getElementById('response');
var p = document.createElement('p');
p.style.wordWrap = 'break-word';
p.appendChild(document.createTextNode(message));
response.appendChild(p);
}
Problems occur when i try to use stomp in Android with libraries like gozirra, activemq-stomp or
others : most of time, connection with server doesn't work. My app stop to run and, after a few
minutes, i have the following message in logcat : java.net.UnknownHostException: Unable to
resolve host "ws://192.168.1.39/client": No address associated with hostname , and i don't
understand why. Code using Gozzira library which manages the stomp appeal in my android
activity :
http://stackoverflow.com/questions/24346068/set-up-a-stomp-client-in-android-with-spring-framework-in-server-side[01/02/2017 00:56:53]
Set up a Stomp client in android with Spring framework in server side - Stack Overflow
try {
c = new Client( ip, port, "", "" );
Log.i("Stomp", "Connection established");
c.subscribe( channel, new Listener() {
public void message( Map header, String message ) {
Log.i("Stomp", "Message received!!!");
}
});
After some research, i found that most persons who want to use stomp over websocket with Java
Client use ActiveMQ server, like in this site. But spring tools are very simple to use and it will be
cool if i could keep my server layer as is it now. Someone would know how to use stomp java
(Android) in client side with Spring configuration in server side?
EPerrin95
264 4 12
you didn't talk about how you are managing threads. F.O.O Jul 17 '16 at 18:22
add a comment
My implementation of STOMP protocol for android (or plain java) with RxJava
https://github.com/NaikSoftware/StompProtocolAndroid. Tested on STOMP server with
http://stackoverflow.com/questions/24346068/set-up-a-stomp-client-in-android-with-spring-framework-in-server-side[01/02/2017 00:56:53]
Set up a Stomp client in android with Spring framework in server side - Stack Overflow
// ...
mStompClient.topic("/topic/greetings").subscribe(topicMessage -> {
Log.d(TAG, topicMessage.getPayload());
});
// ...
mStompClient.disconnect();
classpath 'me.tatarka:gradle-retrolambda:3.2.0'
android {
.............
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
............................
compile 'org.java-websocket:Java-WebSocket:1.3.0'
compile 'com.github.NaikSoftware:StompProtocolAndroid:1.1.5'
}
All working asynchronously! You can call connect() after subscribe() and send() , messages
will be pushed to queue.
Additional features:
extra HTTP headers for handshake query (for passing auth token or other)
you can implement own transport for library, just implement interface ConnectionProvider
subscribe connection lifecycle events (connected, closed, error)
http://stackoverflow.com/questions/24346068/set-up-a-stomp-client-in-android-with-spring-framework-in-server-side[01/02/2017 00:56:53]
Set up a Stomp client in android with Spring framework in server side - Stack Overflow
For Example :
mStompClient = Stomp.over(WebSocket.class,
"ws://localhost:8080/app/hello/websocket");
mStompClient.connect();
mStompClient.topic("/topic/greetings").subscribe(topicMessage -> {
Log.d(TAG, topicMessage.getPayload());
});
mStompClient.lifecycle().subscribe(lifecycleEvent -> {
switch (lifecycleEvent.getType()) {
case OPENED:
Log.d(TAG, "Stomp connection opened");
break;
http://stackoverflow.com/questions/24346068/set-up-a-stomp-client-in-android-with-spring-framework-in-server-side[01/02/2017 00:56:53]
Set up a Stomp client in android with Spring framework in server side - Stack Overflow
How do you reconnect with your library? Thanks. File Aug 9 '16 at 11:24
1 can't use it. everytime StompClient receive message, it always disconnected. Raizal Nov 9 '16 at 4:42
1 @Raizal call connect() method after subscibe Nickolay Savchenko Nov 9 '16 at 8:55
add a comment
I achieve to use stomp over web socket with Android and spring server.
6 To do such a thing, i used a web socket library : werbench (follow this link to download it). To
install, I used the maven command mvn install and i got back the jar in my local repository.
Then, I need to add a stomp layer on the basic web socket one, but i couldn't find any stomp
library in java which could manage stomp over web socket (I had to give up gozzira). So I create
my own one (with stomp.js like model). Don't hesitate to ask me if you want take a look at it, but i
realized it very quickly so it can not manage as much as stomp.js. Then, i need to realize an
authentication with my spring server. To achieve it, i followed the indication of this site. when i get
back the JSESSIONID cookie, I had just need to declare an header with this cookie in the
instantiation of a werbench web socket in my stomp "library".
EDIT : this is the main class in this library, the one which manage the stomp over web socket
connection :
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import android.util.Log;
import de.roderick.weberknecht.WebSocket;
import de.roderick.weberknecht.WebSocketEventHandler;
import de.roderick.weberknecht.WebSocketMessage;
http://stackoverflow.com/questions/24346068/set-up-a-stomp-client-in-android-with-spring-framework-in-server-side[01/02/2017 00:56:53]
Set up a Stomp client in android with Spring framework in server side - Stack Overflow
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Constructor of a Frame object. All parameters of a frame can be instantiate
*
* @param command
* @param headers
* @param body
*/
public Frame(String command, Map<String, String> headers, String body){
this.command = command;
this.headers = headers != null ? headers : new HashMap<String, String>();
this.body = body != null ? body : "";
}
http://stackoverflow.com/questions/24346068/set-up-a-stomp-client-in-android-with-spring-framework-in-server-side[01/02/2017 00:56:53]
Set up a Stomp client in android with Spring framework in server side - Stack Overflow
return command;
}
This one is an object used to establish a subscription via the stomp protocol :
At least, there are two interfaces used as the "Run" java class, to listen web socket network and a
given subscription canal
import java.util.Map;
public interface ListenerSubscription {
http://stackoverflow.com/questions/24346068/set-up-a-stomp-client-in-android-with-spring-framework-in-server-side[01/02/2017 00:56:53]
Set up a Stomp client in android with Spring framework in server side - Stack Overflow
public void onMessage(Map<String, String> headers, String body);
}
EPerrin95
264 4 12
2 You should always post your solution. Anders Metnik Sep 15 '14 at 12:19
1 Could you please provide an example about how to subscribe to ws service? ismail Feb 9 '15 at 0:31
Hi, How do you manage to run Spring over Cross domain (mobile devices). I am implementing a solution
using Spring with Sockjs and JWT tokens but Sockjs fallback into iframe because (I think) the client is
running in a different domain. cardeol Apr 5 '16 at 21:15
add a comment
Perfect solution Eperrin thank you. I would like fill the complete solution e.g. in your
Activity/Service phase you call connection method of course doesn't in MainThread.
1
private void connection() { Map<String,String> headersSetup = new HashMap<String,String>();
Stomp stomp = new Stomp(hostUrl, headersSetup, new ListenerWSNetwork() { @Override public
void onState(int state) { } }); stomp.connect(); stomp.subscribe(new Subscription(testUrl,
new ListenerSubscription() { @Override public void onMessage(Map<String, String> headers,
String body) { } })); }
horkavlna
1,884 1 9 12
http://stackoverflow.com/questions/24346068/set-up-a-stomp-client-in-android-with-spring-framework-in-server-side[01/02/2017 00:56:53]
Set up a Stomp client in android with Spring framework in server side - Stack Overflow
My server also have path to SEND messages (chat broker). So how can I send something to it with that
solution? udenfox Mar 31 '15 at 15:21
I will try explain solution for AsyncTask the same you can do it in Services. First you have to create
Activity/Fragment, after you call AsyncTask and in phase doInBackground you call my method connection
(in previous answered). And you have to import weberknecht library in your project because you call object
Stomp which exist in this library thos is everything. But be careful your server must support STOMP version
websocket horkavlna Mar 31 '15 at 15:39
Okay, I got It. Reading code a little bit carefully makes me understand. Thanks! udenfox Mar 31 '15 at
16:47
Don't forget send PING server, default implementation doesn't send PING and you can get EOF after few
doesn't send PING ...it depends implementation on server. horkavlna Nov 4 '15 at 14:42
add a comment
Your Answer
http://stackoverflow.com/questions/24346068/set-up-a-stomp-client-in-android-with-spring-framework-in-server-side[01/02/2017 00:56:53]
Set up a Stomp client in android with Spring framework in server side - Stack Overflow
By posting your answer, you agree to the privacy policy and terms of service.
Not the answer you're looking for? Browse other questions tagged android spring websocket
question feed
about us tour help blog chat data legal privacy policy work here advertising info developer jobs directory mobile contact us feedback
CULTURE /
TECHNOLOGY LIFE / ARTS SCIENCE OTHER
RECREATION
Stack Overflow Geographic Information Code Review Photography English Language & MathOverflow Meta Stack Exchange
Systems Usage
Server Fault Magento Science Fiction & Mathematics Stack Apps
Electrical Engineering Fantasy Skeptics
Super User Signal Processing Cross Validated (stats) Area 51
Android Enthusiasts Graphic Design Mi Yodeya (Judaism)
Web Applications Raspberry Pi Theoretical Computer Stack Overflow Talent
Information Security Movies & TV Travel Science
Ask Ubuntu Programming Puzzles &
Database Administrators Code Golf Music: Practice & Theory Christianity Physics
Webmasters
Drupal Answers more (7) Seasoned Advice English Language Chemistry
Game Development (cooking) Learners
SharePoint Biology
TeX - LaTeX Home Improvement Japanese Language
User Experience Computer Science
Software Engineering Personal Finance & Arqade (gaming)
Mathematica Money Philosophy
Unix & Linux Bicycles
Salesforce Academia more (3)
Ask Different (Apple) Role-playing Games
ExpressionEngine more (8)
WordPress Development Answers Anime & Manga
more (17)
http://stackoverflow.com/questions/24346068/set-up-a-stomp-client-in-android-with-spring-framework-in-server-side[01/02/2017 00:56:53]
Set up a Stomp client in android with Spring framework in server side - Stack Overflow
site design / logo 2017 Stack Exchange Inc; user contributions licensed under cc by-sa 3.0 with attribution required
rev 2017.1.31.24871
http://stackoverflow.com/questions/24346068/set-up-a-stomp-client-in-android-with-spring-framework-in-server-side[01/02/2017 00:56:53]