cemartins / websocket-test Goto Github PK
View Code? Open in Web Editor NEWsetup websocket with spring framework 3.x
setup websocket with spring framework 3.x
Hi Carlos !
Thanks for this POC.
I use something like this :
url = 'ws://' + window.location.host + '/ws/mypathparam/?aQueryParam=123&anotherQueryParam=abc'
ws = new WebSocket(url);
@ServerEndpoint(value = "/ws/{aParam}")
public class myPublicClass {
[...]
@OnOpen
public void onOpen(@PathParam("aParam") String aParam, Session session, EndpointConfig config) {
Map<String, String> queryParams = getQueryMap(session.getQueryString());
if (queryParams.containsKey("aQueryParam")) {
theQueryParam= queryParams.get("aQueryParam");
System.out.println(theQueryParam);
}
[...]
public static Map<String, String> getQueryMap(String query) {
Map<String, String> map = Maps.newHashMap();
if (query != null) {
String[] params = query.split("&");
for (String param : params) {
String[] nameval = param.split("=");
map.put(nameval[0], nameval[1]);
}
}
return map;
}
I was wondering how could we go with 'path parameters' and 'query Parameters' in your solution ?
can i only use your SpringConfigurator.java without MyApplication.java ( do not inittail by the servlet but the spring)
like this ? it seems that it doesnot workπ
@serverendpoint(value="/ws/createfile/",configurator=SpringConfigurator.class)
I tried to store session in MyEndpoint
or another helper class when connection is established in several ways.
1. private static final Set<Session> sessions = Collections
.synchronizedSet(new HashSet<Session>());
2. public static final List<Session> users = new ArrayList<Session>();
3. public static final Map<String,Session> clients = new ConcurrentHashMap<String,Session>();
@Override
public void onOpen(Session session, EndpointConfig config) {
System.out.println("websocket connection opened: " + session.getId());
session.getAsyncRemote().sendText("connected!!");
WSSessionUtils.put(session.getId(), session);
WSSessionUtils.add(session);
sessions.add(session);
// MyPongHandler pongHandler = new MyPongHandler(session);
// session.addMessageHandler(new MyMessageHandler(session,
// pongHandler));
// session.addMessageHandler(pongHandler);
}
Then broadcast message to sessions like below.
public static void broadcast() {
synchronized (sessions) {
for (Session s : sessions) {
if (s.isOpen()) {
try {
// send the article summary to all the connected clients
s.getBasicRemote().sendText("yo ");
} catch (IOException ex) {
}
}
}
}
}
But every time the collection of session will be empty. Can you help to have a look?
Hi, I'm trying to apply the PoC you did in a real app.
My app is already configured with Spring 3.2, it had a web.xml with a servlet for the root (path /), and is configured to read @configuration too.
So, when I load the classes created in your PoC in my app, it's failing in the MyApplication trying to get the attribute "javax.websocket.server.ServerContainer" from the container.
Can you explain me how this is suppose to be populated?
I've check your project and I can't find any configuration for that.
Any help will be very much appreciated.
Thanks
A declarative, efficient, and flexible JavaScript library for building user interfaces.
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. πππ
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google β€οΈ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.