You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by bu...@apache.org on 2014/06/25 23:45:55 UTC

[Bug 56673] New: Tomcat Websocket 8.0.8 Java Standalone Client Session.getId() returns "0"

https://issues.apache.org/bugzilla/show_bug.cgi?id=56673

            Bug ID: 56673
           Summary: Tomcat Websocket 8.0.8 Java Standalone Client
                    Session.getId() returns "0"
           Product: Tomcat 8
           Version: 8.0.8
          Hardware: PC
            Status: NEW
          Severity: major
          Priority: P2
         Component: WebSocket
          Assignee: dev@tomcat.apache.org
          Reporter: tellercapital@gmail.com

I'm currently testing various Java websocket standalone client containers with
a simple test application where the standalone Java client app makes a
websocket connection to a Wildfly 8.0.0.Final app server running on the same
machine.

For the Tomcat client container, the client app uses the following Maven
dependencies:

<dependency>
    <groupId>javax.websocket</groupId>
    <artifactId>javax.websocket-api</artifactId>
    <version>1.0</version>
</dependency>
<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-websocket</artifactId>
    <version>8.0.8</version>
</dependency>
<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-coyote</artifactId>
    <version>8.0.8</version>
</dependency>

Everything works fine:  the client is able to successfully connect to the
server, send messages and receive messages.  The only problem that I've
encountered is that on each of the callback methods on the client, the Session
parameter doesn't contain the correct Session id, i.e. calling session.getId()
returns "0".  On the server side, the callback session params contain the
session id, so I know that it being created correctly on the server, just not
sent to the client.

******************************************************
CLIENT CODE:
@ClientEndpoint()
public class MyClientEndpoint
{
    private static CountDownLatch latch;

    public void connect(URI endpointURI)
    {
        try
        {
            WebSocketContainer container =
ContainerProvider.getWebSocketContainer();
            System.out.println("container = " + container);
            container.connectToServer(MyClientEndpoint.class, endpointURI);
        }
        catch (Exception e)
        {
            System.out.println("Exception trying to connect");
            latch.countDown();
            throw new RuntimeException(e);
        }
    }

    @OnMessage
    public void onMessage(Session pSession, String message)
    {
        System.out.println("Received message: " + message + " from Session: " +
pSession.getId());
        if (message.equalsIgnoreCase("goodbye"))
        {
            try
            {
                pSession.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }

    @OnOpen
    public void onOpen(Session pSession)
    {
        System.out.println("Session Opened Successfully: " + pSession.getId());
        System.out.println("Session class = " + pSession.getClass().getName());
        try
        {
            pSession.getBasicRemote().sendText("Hello!");
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    @OnClose
    public void onClose(Session pSession)
    {
        System.out.println("Session Closed: " + pSession.getId());
        latch.countDown();
    }

    @OnError
    public void onError(Session pSession, Throwable pThrowable)
    {
        System.out.println("Received Error: ");
        try
        {
            pSession.close();
        }
        catch (IOException e)
        {
        }
        latch.countDown();
        pThrowable.printStackTrace();
    }

    public static void main(String[] args)
    {
        latch = new CountDownLatch(1);
        String uriString = "ws://localhost/test";
        try
        {
            URI uri = new URI(uriString);
            SampleClientEndpoint client = new SampleClientEndpoint();
            client.connect(uri);
            latch.await();

        }
        catch (URISyntaxException | InterruptedException e)
        {
            throw new RuntimeException(e);
        }
    }
}

******************************************************
SERVER CODE:
@ServerEndpoint("/test")
public class MyServerEndpoint
{
    @OnMessage
    public void onMessage(Session pSession, String message)
    {
        System.out.println("Received message: " + message + " from Session: " +
pSession.getId());
    }

    @OnOpen
    public void onOpen(Session pSession)
    {
        System.out.println("Session Opened Successfully: " + pSession.getId());
        try
        {
            for (int i = 1; i < 5; i++)
            {
                pSession.getBasicRemote().sendText("hello" + i);
            }
            pSession.getBasicRemote().sendText("goodbye");
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    @OnClose
    public void onClose(Session pSession)
    {
        System.out.println("Session Closed: " + pSession.getId());
    }

    @OnError
    public void onError(Session pSession, Throwable pThrowable)
    {
        System.out.println("Received Error For Session: " + pSession.getId());
        pThrowable.printStackTrace();
    }
}

-- 
You are receiving this mail because:
You are the assignee for the bug.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


[Bug 56673] Tomcat Websocket 8.0.8 Java Standalone Client Session.getId() returns "0"

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=56673

Konstantin Kolinko <kn...@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |INVALID
                 OS|                            |All

--- Comment #1 from Konstantin Kolinko <kn...@gmail.com> ---
(In reply to John Teller from comment #0)
> 
> <dependency>
>     <groupId>javax.websocket</groupId>
>     <artifactId>javax.websocket-api</artifactId>
>     <version>1.0</version>
> </dependency>

1. You should use the API jar provided by Tomcat instead of the above one. That
would be

<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-websocket-api</artifactId>
    <version>8.0.8</version>
</dependency>


2. Your expectations are wrong. There is no error here.

There is no session in WebSocket protocol (RFC6455). A Session is just a way to
represent an established connection in Java API.

If both ends of the connection are implemented in Java, each end has its own
implementation of Session with its own ID. The Session ID is not transmitted
across the wire. Tomcat uses a counter that starts with "0".

// WsSession.java
> this.id = Long.toHexString(ids.getAndIncrement());

3. Bugzilla is not a support forum.

-- 
You are receiving this mail because:
You are the assignee for the bug.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org