# Websocket nach Decoder Annotation nicht mehr erreichbar



## denis7788 (14. Aug 2015)

Hi!

Nachdem ich in der ServerEndpoint Anotation Decoder und Encoder angebe, bekomme ich ein Handshake Error vom Server zurück. Wenn ich nur folgendes angebe funktioniert es ohne Probleme:

```
@ServerEndpoint("/mychat")
```

Hier der Log:

```
javax.websocket.DeploymentException: Handshake error.
at org.glassfish.tyrus.client.ClientManager$3$1.run(ClientManager.java:674)
at org.glassfish.tyrus.client.ClientManager$3.run(ClientManager.java:712)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at org.glassfish.tyrus.client.ClientManager$SameThreadExecutorService.execute(ClientManager.java:866)
at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:112)
at org.glassfish.tyrus.client.ClientManager.connectToServer(ClientManager.java:511)
at org.glassfish.tyrus.client.ClientManager.connectToServer(ClientManager.java:373)
at de.denis.mywebsockets.client.ChatClientEndpoint.connectToEndpoint(ChatClientEndpoint.java:52)
at de.denis.mywebsockets.client.ChatClientEndpoint.<init>(ChatClientEndpoint.java:30)
at de.denis.mywebsockets.client.ChatClientUserInterface.main(ChatClientUserInterface.java:14)
Caused by: org.glassfish.tyrus.core.HandshakeException: Response code was not 101: 404.
at org.glassfish.tyrus.client.TyrusClientEngine.processResponse(TyrusClientEngine.java:320)
at org.glassfish.tyrus.container.grizzly.client.GrizzlyClientFilter.handleHandshake(GrizzlyClientFilter.java:346)
at org.glassfish.tyrus.container.grizzly.client.GrizzlyClientFilter.handleRead(GrizzlyClientFilter.java:315)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
at java.lang.Thread.run(Thread.java:745)
```

Hier der ServerEndpoint:

```
package de.denis.websockets.endpoint;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

import de.denis.mywebsockets.decoders.*;
import de.denis.mywebsockets.encoders.*;


@ServerEndpoint(value = "/mychat",
encoders = {ChatMessageEncoder.class},
decoders = {ChatMessageDecoder.class})

public class MyChatEndpoint {
 
    private static Set<Session> sessions =
            Collections.synchronizedSet(new HashSet<Session>());
 
    @OnOpen
    public void onOpen(Session session){
        sessions.add(session); 
    }
 
    @OnMessage
    public void onMessage(Session session, String message){
        for (Session client : sessions) {
            message = "Hallooo, " + message;
            client.getAsyncRemote().sendText(message);        }
     
     
    }
 
    @OnClose
    public void onClose(Session session){
        sessions.remove(session);
    }
 
     
}
```

Der Client:

```
package de.denis.mywebsockets.client;

import java.io.IOException;
import java.net.URI;

import javax.websocket.ClientEndpoint;
import javax.websocket.ContainerProvider;
import javax.websocket.DeploymentException;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.WebSocketContainer;

import de.denis.mywebsockets.decoders.*;
import de.denis.mywebsockets.encoders.*;


@ClientEndpoint (
        encoders = ChatMessageEncoder.class,
        decoders = ChatMessageDecoder.class)

public class ChatClientEndpoint {
 
    private Session session = null;
    private URI endpointURI;
 
    //Constructor
    public ChatClientEndpoint (URI endpointURI) {
        this.connectToEndpoint(endpointURI);
    }
 
    @OnOpen
    public void onOpen(Session session) {
        this.session = session;
    }
 
 
    @OnMessage
    public void onMessage(String receivingMessage) {
        System.out.println("Message from Server: " + receivingMessage);
    }
 
    @OnClose
    public void onClose() {
        this.setSession(null);
    }
 
    public void connectToEndpoint (URI endpointURI) {
        WebSocketContainer container = ContainerProvider.getWebSocketContainer();
        try {
            container.connectToServer(this, endpointURI);
        } catch (DeploymentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
 
    public void sendMessage (String message) {
        System.out.println("Message in sendMessage is: " + message);
        System.out.println("Session: " + session.getId());
        session.getAsyncRemote().sendText(message);
    }

    public Session getSession() {
        return session;
    }

    public void setSession(Session session) {
        this.session = session;
    }

    public URI getEndpointURI() {
        return endpointURI;
    }

    public void setEndpointURI(URI endpointURI) {
        this.endpointURI = endpointURI;
    }
 
}
```

Und das "Interface":

```
package de.denis.mywebsockets.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;

public class ChatClientUserInterface {
 
    public static void main (String[] args) throws URISyntaxException, IOException {
     
        ChatClientEndpoint endPoint =
                new ChatClientEndpoint(new URI ("ws://localhost:8080/MyWebSocketsWeb/mychat"));
     
     
     
     
        while (true) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            String message = reader.readLine();
            if (message.equals("stop")) {
                break;
            }
            if (endPoint != null) {
                System.out.println("MEssage is: " + message);
            }
         
            endPoint.sendMessage(message);
         
        }
     
    }

}
```


----------

