0

I have chat which mapping to static URL. I need get the opportunity creating rooms for user.

How to inject variable in annotation @ServerEndpoint("/myVariable") when app already running?

class SomeClass{
    public void createRoom(User user) {
        String name = user.getName();
        //...Somehow inject name to annotation @ServerEndpoint("/name")...
    }
}

@ServerEndpoint("/chat") //May be replace to @ServerEndpoint(someGetteUserName())
public class ChatEndpoint {
    @OnMessage
    public void message(String message, Session client)
            throws IOException, EncodeException {

        for (Session peer : client.getOpenSessions()) {
            peer.getBasicRemote().sendText(message);
        }
    }
}

I don't use Spring this is clear websocket and Glassfish.

Help me create implementation variable injection to annotation. Thank You.

Pavel
  • 2,005
  • 5
  • 36
  • 68

1 Answers1

1

I think that you don't need any injection if you only want to create and handle chat rooms. You just need to handle this by java code independently from your endpoint.

I recommend you to:

  • Create one websocket server endpoint: @ServerEndpoint("/chat"/{client_id}). This client id pathParam is may serve as a session id.
  • In ChatEndpoint class, initialize a list of rooms (this list should be static <=> common between all threads).
  • Create your business methods to handle clients and rooms(create/delete user, create/delete room, subscribe to a room...etc).
  • Finally, in your chat message try to specify the room destination. This can be very simple if you use JSON format.

message = { ... ,"room": "room1", ... }

Karim G
  • 448
  • 3
  • 9
  • Thank You for answer. I understeand all except in `@ServerEndpoint("/chat"/{client_id})` part `{client_id}` what is mean? This is param in request from user or part of path? In you're example `"/chat"/{client_id}` path is `"/chat"` but that mean syntax with breacket `{client_id}`? How inject this param on client side and how to get in my ChatEndpoint class? – Pavel Jul 10 '17 at 09:51
  • It is just a suggestion to identify a client session by it's id which can be helpful when pushing message to a specific receiver. For example a first session A want to directly send a message to an other session B (A chat in private with B). Here (https://stackoverflow.com/a/21572502/3834429) is some useful source code (front & back) :) – Karim G Jul 10 '17 at 10:06
  • This way suggests assert what Client know her id? – Pavel Jul 10 '17 at 18:31