title: How to send message to a specific device by spring websocket
body: <p>I am fairly new with Spring websocket. I have a simple demo below:</p>
  
  <p>HandshakeInterceptor class:</p>
  
  <pre><code>public class HandshakeInterceptor extends HttpSessionHandshakeInterceptor {
  
  private static final Log logger = LogFactory
          .getLog(HandshakeInterceptor.class);
  
  @Override
  public boolean beforeHandshake(ServerHttpRequest request,
          ServerHttpResponse response, WebSocketHandler wsHandler,
          Map&lt;String, Object&gt; attributes) throws Exception {
      logger.info("Before Handshake");
      return super.beforeHandshake(request, response, wsHandler, attributes);
  }
  
  @Override
  public void afterHandshake(ServerHttpRequest request,
          ServerHttpResponse response, WebSocketHandler wsHandler,
          Exception ex) {
      logger.info("After Handshake");
      super.afterHandshake(request, response, wsHandler, ex);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>WebsocketEndPoint class:</p>
  
  <pre><code>public class WebsocketEndPoint extends TextWebSocketHandler {
  
  private static final Log logger = LogFactory
          .getLog(WebsocketEndPoint.class);
  
  @Override
  protected void handleTextMessage(WebSocketSession session,
          TextMessage message) throws Exception {
      super.handleTextMessage(session, message);
      logger.info("Recieved Message: " + message.getPayload());
      TextMessage returnMessage = new TextMessage(message.getPayload()
              + " received at server");
      logger.info("Return Message: " + returnMessage.getPayload());
      session.sendMessage(returnMessage);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Spring mapping websocket:</p>
  
  <pre><code>&lt;bean id="websocket"
      class="com.n9.api.websocket.handler.WebsocketEndPoint" /&gt;
  
  &lt;websocket:handlers&gt;
      &lt;websocket:mapping path="/websocket" handler="websocket" /&gt;
      &lt;websocket:handshake-interceptors&gt;
          &lt;bean
              class="com.n9.api.websocket.HandshakeInterceptor" /&gt;
      &lt;/websocket:handshake-interceptors&gt;
  &lt;/websocket:handlers&gt;
  </code></pre>
  
  <p>Html:</p>
  
  <pre><code>function connect() {
          if ('WebSocket' in window){
                console.log('Websocket supported');
                socket = new WebSocket('ws://localhost:8080/n9-api/websocket');
  
                console.log('Connection attempted');
  
                socket.onopen = function(){
                     console.log('Connection open!');
                }
  
                socket.onclose = function(){
                    console.log('Disconnecting connection');
                }
  
                socket.onmessage = function (evt) 
                   { 
                      var received_msg = evt.data;
                      console.log(received_msg);
                      console.log('message received!');
                   }
  
              } else {
                console.log('Websocket not supported');
              }
      }
  
      function disconnect() {
          setConnected(false);
          console.log("Disconnected");
      }
  
      function sendMessage(message) {
          socket.send(JSON.stringify({ 'message': message }));
      }
  </code></pre>
  
  <p>Now i want to send a message to a specific user. I define each device have a deviceId, so i want to know:</p>
  
  <ol>
  <li>How to handshake with attributes, which will be send from client?</li>
  <li>How to send handshake attributes from client javascript?</li>
  <li>How to send message from server to client by handshake attributes?</li>
  </ol>
  
  <p>Thank you for your attention.</p>