Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting errors in Websocket server Endpoint

Tags:

java

websocket

This is file and I am getting compilation error in import javax.websocket lines and in @serverEndpoint("/websocket"). Why it is not taking the annotation?

package pack.exp;
import java.io.IOException;
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;

@ServerEndpoint("/websocket")
public class Hello 
{
{

      private static Set<Session> clients = 
        Collections.synchronizedSet(new HashSet<Session>());

      @OnMessage
      public void onMessage(String message, Session session) 
        throws IOException {

        synchronized(clients){
          // Iterate over the connected sessions
          // and broadcast the received message
          for(Session client : clients){
            if (!client.equals(session)){
              client.getBasicRemote().sendText(message);
            }
          }
        }

      }

      @OnOpen
      public void onOpen (Session session) {
      // Add session to the connected sessions set
        clients.add(session);
      }

      @OnClose
      public void onClose (Session session) {
        // Remove session from the connected sessions set
        clients.remove(session);
      }

    }
}

Please Help me with this error. Are there some specific api which I have to implement in this code?

like image 339
Sandeep Avatar asked Jan 10 '14 07:01

Sandeep


2 Answers

The missing classes are part of the java ee 7 api. If you are building your project with maven, take a look at the following repository

http://mvnrepository.com/artifact/javax/javaee-api/7.0

and add this dependency to your project:

<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>

If you are not using maven, you can download the jar from the page above. http://repo1.maven.org/maven2/javax/javaee-api/7.0/javaee-api-7.0.jar

Then you have the API.

like image 90
Wintermute Avatar answered Oct 26 '22 10:10

Wintermute


The classes under the javax.websocket are defined by the JSR-356 to be standalone. They can run on a completely standalone server or from within a Java EE 7 container.

If you are using just javax.websocket and don't care about the rest of Java EE 7, then just use the official javax.websocket artifacts in a provided scope.

Here's the search lookup directly to the artifact.

https://search.maven.org/artifact/javax.websocket/javax.websocket-api/1.0/bundle

That is the official search page for the maven central repository system, it also includes build system references to that artifact for maven, grails, ivy, buildr, grape, and sbt.

The maven pom reference would be:

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

Make sure you do not include this artifact in your *.war file, as this functionality will be provided by whatever container you decide to deploy into. (Such as Eclipse Jetty 9.1+ or Apache Tomcat 8.0+)

like image 39
Joakim Erdfelt Avatar answered Oct 26 '22 12:10

Joakim Erdfelt