FPGA
FPGA

Reputation: 3865

JAVA EE 7 WebSocket implementation

In nodejs socket.io uses a single socket connection for all modules, does this apply to the java websocket implementation?

for example

should i make multiple ServerEndPoints

@ServerEndpoint("/ws1")
public class Socket1 {}

@ServerEndpoint("/ws2")
public class Socket2 {}

@ServerEndpoint("/ws3")
public class Socket3 {}

will a single socket connection be used to handle all of them?

or sholud i use a single ServerEndpoint and do actions based on my message type such as

    @ServerEndpoint(value = "/ws",
                    encoders = CommandEncoder.class,
                    decoders = CommandDecoder.class)
        public class Socket {
           @OnMessage
           public void onMessage(Command message, Session session){
                switch(message.type){
                   case SomeAction:dosomething(message,session);break;
                   case AnotherAction:doAnotherThing(message,session);break; 
                }
           }

    }

Upvotes: 0

Views: 319

Answers (1)

tmarwen
tmarwen

Reputation: 16354

I can only say that from a pure personal point of view, both ways you suggested for handling incoming messages are legal.

But apart of those (even though this may not meet your requirements), You can use the URI path templating to specify a variable embeded in your URI. Here you will have only one ServerEndpoint then retrieve the path variable and check it in order to pick up the service you want to flow based on the substiuted parameter.

@ServerEndpoint(value = "/ws/{wsQualifier}",
                encoders = CommandEncoder.class,
                decoders = CommandDecoder.class) {

  @OnMessage
  public void onMessage(Command message,
                          Session session,
                          @PathParam("wsQualifier") int ws) {
    switch(ws) {
      case 1:dosomething(message,session);break;
      case 2:doAnotherThing(message,session);break; 
    }
  }
}

Upvotes: 2

Related Questions