2014-11-06 56 views
6

我正在開發簡單的應用程序以幫助我學習Java中的WebSocket和Tyrus概念。這裏是我的服務器端(服務器端點)和我的pom.xml和我的客戶端和pom.xml中:Tyrus - 簡單的java應用程序 - 找不到實現類

服務器端

@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內部

回答

13

您有依賴關係問題。這一個:

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

是不正確的。您需要使用[1](灰熊基於客戶端):

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

[2](JDK 7 NIO客戶IMPL)

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

非常感謝您!它解決了我的問題。 :) – Sysrq147 2014-11-07 15:01:22

+0

沒問題,我很高興它幫助;) – 2014-11-10 08:41:49

+0

有沒有相應的JAR,因爲我沒有使用Maven? – Antinous 2017-06-13 08:31:22

相關問題