Hello, I need to implement a WebSocket client-server, where the server is on .Net and the Fleck library, and the client is in JavaScript in the browser. Everything works, but I need to make periodic notifications from the Server to certain clients. not all connected clients, but to some one (for example, by id)? Roughly speaking, it is something like WhatsApp, Viber, where id is the user's phone number. How can the client be tied tightly at the first connection to which id, and then send it to the client from the server.

  • On the server there is a "list" of all connections on ws, according to this list the distribution takes place. A "list" can contain only ws connections. It is necessary to organize a new "list" which will contain id / im_client - ws_connect. From this list, by id, ws_connect is selected and sent via it. How to do it on java, I would show ... - Vadim
  • Show Java please - GoodRA

1 answer 1

tomcat library for ws is used, there is a login page on the portal; the user login is fixed on it, it is used later. there is a configurator

public class WsServerConfigurator extends ServerEndpointConfig.Configurator { @Override public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) { sec.getUserProperties().put("username", (String) ((HttpSession) request.getHttpSession()).getAttribute(PARAM_LOGIN)); sec.getUserProperties().put("sessionID", (String) ((HttpSession) request.getHttpSession()).getId()); sec.getUserProperties().put("session", (HttpSession) request.getHttpSession()); } } 

and ws server

  @ServerEndpoint(value = "/WS_server", configurator = WsServerConfigurator.class) public class WS_server { public static Set<Session> ListWsSocet = Collections.synchronizedSet(new HashSet<Session>()); @OnOpen public void OnOpen(EndpointConfig endpointConfig, Session userSession) { userSession.getUserProperties().put("username", endpointConfig.getUserProperties().get("username")); userSession.getUserProperties().put("sessionID", endpointConfig.getUserProperties().get("sessionID")); userSession.getUserProperties().put("session", endpointConfig.getUserProperties().get("session")); ListWsSocet.add(userSession); } 

there is a method to send a message to the selected user

  public void SendMessage(String username, String message) { ListWsSocet.stream().forEach((Session x) -> { if (x.getUserProperties().containsValue(username)) { try { x.getBasicRemote().sendText(message); return; } catch (IOException ex) { } } }); } 

I have a rather tricky processing of input messages from clients :) (this is a separate topic). On onMessage, a data processing method is called, in which you can call SendMessage with the appropriate parameters.

  @OnMessage public void OnMessage(String data, Session userSession) { // пропущено .... try { cl = Class.forName(Singleton.ListCommand.get(command_).getClass().getName()); method = cl.getMethod(command_, java.lang.String.class, Session.class); method.invoke(Singleton.ListCommand.get(command_), data_, userSession); } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | ClassNotFoundException ex) { ex.printStackTrace(); } }