Guest User

Untitled

a guest
Feb 21st, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.net.URI;
  3.  
  4. import javax.websocket.*;
  5.  
  6. @ClientEndpoint
  7. public class Client {
  8.  
  9. Session session;
  10.  
  11. private final static String url = "ws://echo.websocket.org";
  12.  
  13. public static void main(String[] args) throws Exception, IOException {
  14. WebSocketContainer container = ContainerProvider.getWebSocketContainer();
  15. System.out.println("connecting...");
  16. container.connectToServer(Client.class,
  17. URI.create(url));
  18. }
  19.  
  20.  
  21. @OnMessage
  22. public void newMessage(String message, Session session) {
  23. System.out.println(message);
  24. }
  25. @OnOpen
  26. public void newConnection(Session session) throws IOException {
  27. this.session = session;
  28. System.out.println("The connection has been started");
  29. session.getBasicRemote().sendText("hello");
  30.  
  31. }
  32. @OnClose
  33. public void disconnection() {
  34. System.out.println("The connection has been ended");
  35. }
  36. }
Add Comment
Please, Sign In to add comment