Created
January 28, 2014 17:48
-
-
Save raphaelLacerda/8672548 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ServerEndpoint("/caelum") | |
public class CaelumServer { | |
@OnOpen | |
public void onOpen(Session cliente) { | |
System.out.println("Novo cliente" + cliente); | |
} | |
@OnClose | |
public void onClose(Session cliente) { | |
System.out.println("Cliente desconectado" + cliente); | |
} | |
@OnMessage | |
public void message(String message, Session client) throws IOException { | |
Set<Session> openSessions = client.getOpenSessions(); | |
for (Session session : openSessions) { | |
session.getBasicRemote().sendText(message); | |
} | |
} | |
} | |
@ClientEndpoint | |
public class ChatClientReceiver { | |
@OnOpen | |
public void onOpen(Session session) { | |
System.out.println("Client Receiver conectado: " + session); | |
} | |
@OnMessage | |
public void processMessage(String message) { | |
System.out.println("Recebendo no Client Receiver: " + message); | |
} | |
} | |
@ClientEndpoint | |
public class ChatClientSender { | |
@OnOpen | |
public void onOpen(Session session) { | |
try { | |
System.out.println("Client Sender conectado: " + session); | |
session.getBasicRemote().sendText("Mensagem Enviada Via WebSocket"); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
@OnMessage | |
public void processMessage(String message) { | |
System.out.println("Recebendo no Client Sender: "+ message); | |
} | |
} | |
public class TestClient { | |
public static void main(String[] args) { | |
WebSocketContainer container = ContainerProvider.getWebSocketContainer(); | |
// String uri = "ws://localhost.bb.com.br:8080/Angular/caelum"; | |
String uri = "ws://127.0.0.1:8082/caelum/websocket"; | |
try { | |
container.connectToServer(ChatClientReceiver.class, URI.create(uri)); | |
Thread.sleep(5000); | |
container.connectToServer(ChatClientSender.class, URI.create(uri)); | |
} catch (DeploymentException | IOException | InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment