Tyrus - 简单的 Java 应用程序 - 找不到实现类

7

我正在开发一个简单的应用程序,帮助我学习Java中WebSocket和Tyrus概念。这是我的服务器端(服务器终端)和pom.xml以及客户端和pom.xml:

服务器端

 @ServerEndpoint(value="/websocket/{client-id}")
    public class MyServerEndpoint {

        private static final LinkedList<Session> clients = new LinkedList<Session>();

        @OnOpen
        public void onOpen(Session session) {
            clients.add(session);

        }
        @OnMessage
        public void onMessage(String message,@PathParam("client-id") String clientId) {
            for (Session client : clients) {
                try {
                    client.getBasicRemote().sendObject(clientId+": "+message);          

                } catch (IOException e) {
                    e.printStackTrace();
                } catch (EncodeException e) {
                    e.printStackTrace();
                }
            }
        }
        @OnClose
        public void onClose(Session peer) {
            clients.remove(peer);
        }
    }

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>TyrusJacpFXServer</groupId>
      <artifactId>TyrusJacpFXServer</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>war</packaging>

      <dependencies>

       <dependency>
        <groupId>javax.websocket</groupId>
        <artifactId>javax.websocket-api</artifactId>
        <scope>compile</scope>
        <version>1.0</version>
     </dependency>

      </dependencies>

      <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
          <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
              <source>1.7</source>
              <target>1.7</target>
            </configuration>
          </plugin>
          <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.4</version>
            <configuration>
              <warSourceDirectory>WebContent</warSourceDirectory>
              <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </project>

客户端

MyClient.java 

@ClientEndpoint
public class MyClient {
    @OnOpen
    public void onOpen(Session session) {
        System.out.println("Connected to endpoint: " + session.getBasicRemote());
        try {
            session.getBasicRemote().sendText("Hello");
        } catch (IOException ex) {
        }
    }

    @OnMessage
    public void onMessage(String message) {
        System.out.println(message);
    }

    @OnError
    public void onError(Throwable t) {
        t.printStackTrace();
    }
}

App.java

public class App {

    public Session session;

    protected void start()
             {

            WebSocketContainer container = ContainerProvider.getWebSocketContainer();

            String uri = "ws://localhost:8080/TyrusJacpFXClient/websocket/desktop-client";
            System.out.println("Connecting to " + uri);
            try {
                session = container.connectToServer(MyClient.class, URI.create(uri));
            } catch (DeploymentException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }             

    }
    public static void main(String args[]){
        App client = new App();
        client.start();

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String input = "";
        try {
            do{
                input = br.readLine();
                if(!input.equals("exit"))
                    client.session.getBasicRemote().sendText(input);

            }while(!input.equals("exit"));

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>TyrusJacpFXClient</groupId>
  <artifactId>TyrusJacpFXClient</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <dependencies>

  <dependency>
    <groupId>javax.websocket</groupId>
    <artifactId>javax.websocket-api</artifactId>
    <scope>compile</scope>
    <version>1.0</version>
 </dependency>

   <dependency>
    <groupId>org.glassfish.tyrus</groupId>
    <artifactId>tyrus-client</artifactId>
    <version>1.8.3</version>
   </dependency>


      <dependency>
            <groupId>org.glassfish.tyrus</groupId>
            <artifactId>tyrus-container-grizzly</artifactId>
            <version>1.1</version>
        </dependency>


  </dependencies>

  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

我遇到了这个错误:

Exception in thread "main" java.lang.RuntimeException: Could not find an implementation class.
at javax.websocket.ContainerProvider.getWebSocketContainer(ContainerProvider.java:73)
at org.hwc.App.start(App.java:25)
at org.hwc.App.main(App.java:40)

有人能帮我看看问题吗?服务器在GlassFish 4.0中运行。

1个回答

14

你有依赖问题。这个问题是:

  <dependency>
        <groupId>org.glassfish.tyrus</groupId>
        <artifactId>tyrus-container-grizzly</artifactId>
        <version>1.1</version>
  </dependency>

不正确。你需要使用[1](基于grizzly的客户端):

  <dependency>
        <groupId>org.glassfish.tyrus</groupId>
        <artifactId>tyrus-container-grizzly-client</artifactId>
        <version>1.8.3</version>
  </dependency>

或者[2](使用jdk 7 NIO客户端实现)

  <dependency>
        <groupId>org.glassfish.tyrus</groupId>
        <artifactId>tyrus-container-jdk-client</artifactId>
        <version>1.8.3</version>
  </dependency>

我没有使用Maven,这个有对应的JAR包吗? - user1300214
http://repo1.maven.org/maven2/org/glassfish/tyrus/bundles/tyrus-standalone-client/1.13.1/tyrus-standalone-client-1.13.1.jar 或 http://repo1.maven.org/maven2/org/glassfish/tyrus/bundles/tyrus-standalone-client-jdk/1.13.1/tyrus-standalone-client-jdk-1.13.1.jar - Pavel Bucek

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接