简体   繁体   English

JAVA EE 7 WebSocket实现

[英]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? 在nodejs中,socket.io对所有模块都使用单个套接字连接,这是否适用于Java websocket实现?

for example 例如

should i make multiple ServerEndPoints 我应该做多个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并根据我的消息类型执行操作,例如

    @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; 
                }
           }

    }

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. 但是除了那些(尽管这可能无法满足您的要求)之外,您可以使用URI路径模板来指定嵌入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然后检索路径变量并进行检查,以便根据替换的参数选择要流动的服务。

@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; 
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM