Olá, estou querendo executar uma aplicação websocket pelo prompt de comando do linux, mas ao executá-la está ocorrendo a seguinte mensagem de erro
> java.lang.RuntimeException: Could not find an implementation class.
> at javax.websocket.ContainerProvider.getWebSocketContainer(ContainerProvider.java:73)
O código estou conseguindo executar pelo Netbeans, mas necessito executá-lo pelo terminal. As minhas classe são
public class Message {
private JsonObject json;
public Message(JsonObject json) {
this.json = json;
}
public JsonObject getJson() {
return json;
}
public void setJson(JsonObject json) {
this.json = json;
}
@Override
public String toString(){
StringWriter writer = new StringWriter();
Json.createWriter(writer).write(json);
return writer.toString();
}
}
public class MessageDecoder implements Decoder.Text {
/**
* Transforma a String de entrada em um objeto do tipo Message
*/
@Override
public Message decode(String string) throws DecodeException {
JsonObject json = Json.createReader(new StringReader(string)).readObject();
return new Message(json);
}
/**
* Checks whether the input can be turned into a valid Message object in
* this case, if we can read it as a Json object, we can.
*/
@Override
public boolean willDecode(String string) {
try {
Json.createReader(new StringReader(string)).read();
return true;
} catch (JsonException ex) {
ex.printStackTrace();
return false;
}
}
/**
* The following two methods are placeholders as we don't need to do
* anything special for init or destroy.
*/
@Override
public void init(EndpointConfig config) {
System.out.println("init");
}
@Override
public void destroy() {
System.out.println("destroy");
}
}
public class MessageEncoder implements Encoder.Text<Message> {
public String encode(Message message) throws EncodeException {
return message.getJson().toString();
}
@Override
public void init(EndpointConfig config) {
System.out.println("Init");
}
@Override
public void destroy() {
System.out.println("destroy");
}
}
@ClientEndpoint
public class WebsocketClient {
WebSocketContainer wsContainer;
Session session;
public WebsocketClient() {
try {
wsContainer = ContainerProvider.getWebSocketContainer();
session = wsContainer.connectToServer(WebsocketClient.class, new URI("ws://localhost:8080/TesteWebSocket3/server"));
} catch (Exception e) {
e.printStackTrace();
}
}
@OnMessage
public void OnMessage(String message) {
System.out.println("Client: " + message);
}
@OnClose
public void onClose(Session userSession) {
this.session = null;
}
public void sendMessage(String message) {
try {
session.getBasicRemote().sendText(message);
} catch (Exception e) {
e.printStackTrace();
}
}
public WebSocketContainer getWsContainer() {
return wsContainer;
}
public void setWsContainer(WebSocketContainer wsContainer) {
this.wsContainer = wsContainer;
}
public Session getSession() {
return session;
}
public void setSession(Session session) {
this.session = session;
}
public static void main(String[] args) {
try {
WebsocketClient ws = new WebsocketClient();
System.out.println("Cliente: " + ws.getSession().getId());
} catch (Exception e) {
e.printStackTrace();
}
}
}
@ServerEndpoint("/server")
public class WebsocketServer {
private static Set<Session> sessions = Collections.synchronizedSet(new HashSet<Session>());
@OnOpen
public void onOpen(Session session) {
System.out.println("New client connections: " + session.getId() + " has opened a connection");
sessions.add(session);
}
@OnMessage
public void message(Session session, String msg) throws DecodeException {
if (session.isOpen()) {
MessageDecoder messageDecoder = new MessageDecoder();
Message message = messageDecoder.decode(msg);
}
}
@OnClose
public void onClose(Session session) {
sessions.remove(session);
}
}