V
V
Vanes Ri_Lax2015-05-18 13:33:21
Java
Vanes Ri_Lax, 2015-05-18 13:33:21

Java + websockets, how to work with clients?

Hello, I took an example here
Here is the code of the server itself:

import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.util.Collections;

import org.java_websocket.WebSocket;
import org.java_websocket.WebSocketImpl;
import org.java_websocket.drafts.Draft;
import org.java_websocket.drafts.Draft_17;
import org.java_websocket.framing.FrameBuilder;
import org.java_websocket.framing.Framedata;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;

public class AutobahnServerTest extends WebSocketServer{
   private static int counter = 0;
  
  public AutobahnServerTest( int port , Draft d ) throws UnknownHostException {
    super( new InetSocketAddress( port ), Collections.singletonList( d ) );
  }
  
  public AutobahnServerTest( InetSocketAddress address, Draft d ) {
    super( address, Collections.singletonList( d ) );
  }

  @Override
  public void onOpen( WebSocket conn, ClientHandshake handshake ) {
    counter++;
    System.out.println( "///////////Opened connection number " + counter );
                
  }

  @Override
  public void onClose( WebSocket conn, int code, String reason, boolean remote ) {
    System.out.println( "closed" );
  }

  @Override
  public void onError( WebSocket conn, Exception ex ) {
    System.out.println( "Error:" );
    ex.printStackTrace();
  }

  @Override
  public void onMessage( WebSocket conn, String message ) {
    conn.send( message );
                
  }

  @Override
  public void onMessage( WebSocket conn, ByteBuffer blob ) {
    conn.send( blob );
  }

  @Override
  public void onWebsocketMessageFragment( WebSocket conn, Framedata frame ) {
    FrameBuilder builder = (FrameBuilder) frame;
    builder.setTransferemasked( false );
    conn.sendFrame( frame );
  }

  public static void main( String[] args ) throws  UnknownHostException {
    WebSocketImpl.DEBUG = false;
    int port;
    try {
      port = new Integer( args[ 0 ] );
    } catch ( Exception e ) {
      System.out.println( "No port specified. Defaulting to 1122" );
      port = 1122;
    }
    new AutobahnServerTest( port, new Draft_17() ).start();
  }
}

The server operation algorithm is as follows: it receives a message from a client and replies to the same client with its own message.
It seems that everything is clear and simple, but I have a few questions:
How can I send a message not back to the client, but to some specific client?
How can I send one message to all clients?
Is there a way to disable a specific client?
Thank you very much in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Viktor Koltcov, 2015-05-18
@vanesxl

Remember the conn list that came to onOpen and remove from the list on onError onClose
Send a message to everyone, you can go through the list c conn

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question