Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.13 KB | None | 0 0
  1. package com.demo.core.ws;
  2.  
  3. import com.demo.core.common.Log;
  4. import io.socket.client.IO;
  5. import io.socket.client.Socket;
  6. import io.socket.emitter.Emitter;
  7.  
  8.  
  9. import org.json.JSONArray;
  10. import org.json.JSONObject;
  11. import org.junit.runner.RunWith;
  12. import org.junit.runners.JUnit4;
  13. import org.testng.annotations.Test;
  14.  
  15. import java.net.URISyntaxException;
  16. import java.util.Map;
  17. import java.util.concurrent.BlockingQueue;
  18. import java.util.concurrent.LinkedBlockingQueue;
  19.  
  20.  
  21. /**
  22.  * Created by igorakintev on 4/21/17.
  23.  */
  24.  
  25. public class SocketIOAPI {
  26.  
  27.     private Socket socket;
  28.  
  29.     @Test()
  30.     public void test() throws URISyntaxException, InterruptedException {
  31.         final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
  32.  
  33.         String localhostURL = "http://localhost:3001";
  34.         String stagingSocketURL = "http://tia-test.us-west-2.elasticbeanstalk.com";
  35.  
  36.         IO.Options opts = new IO.Options();
  37.         opts.forceNew = true;
  38.         opts.reconnection = false;
  39.  
  40.         socket = IO.socket(stagingSocketURL, opts);
  41.  
  42.         socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
  43.             @Override
  44.             public void call(Object... objects) {
  45.                 Log.info("Connected " + socket.id());
  46.  
  47.                 JSONArray obj = new JSONArray();
  48.                 JSONArray obj1 = new JSONArray();
  49.                 obj.put("uuid");
  50.                 obj1.put("4532363456456");
  51.                 obj.put(obj1);
  52.  
  53.                 socket.emit("Authentication", obj);
  54. //              socket.emit("TiaMessage", "{'uuid', {'21424534654'}}");
  55.             }
  56.         });
  57.         socket.on(Socket.EVENT_CONNECTING, new Emitter.Listener() {
  58.             @Override
  59.             public void call(Object... objects) {
  60.                 Log.info("Connecting...");
  61.             }
  62.         });
  63.         socket.on(Socket.EVENT_CONNECT_TIMEOUT, new Emitter.Listener() {
  64.             @Override
  65.             public void call(Object... objects) {
  66.                 Log.info("Connection timeout");
  67.             }
  68.         });
  69.         socket.on(Socket.EVENT_CONNECT_ERROR, new Emitter.Listener() {
  70.             @Override
  71.             public void call(Object... objects) {
  72.                 Log.info("Connection error");
  73.             }
  74.         });
  75.         socket.on(Socket.EVENT_MESSAGE, new Emitter.Listener() {
  76.             @Override
  77.             public void call(Object... objects) {
  78.                 Log.info("New message");
  79.             }
  80.         });
  81.         socket.on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {
  82.             @Override
  83.             public void call(Object... objects) {
  84.                 Log.info("Disconected event");
  85.             }
  86.         });
  87.         socket.on("Authentication", new Emitter.Listener() {
  88.             @Override
  89.             public void call(Object... args) {
  90.                 Log.info("Auth!");
  91.                 for (int i = 0; i < args.length; i++) {
  92.                     System.out.println(args[i]);
  93.                 }
  94.             }
  95.         });
  96.         socket.on("ErrorMessage", new Emitter.Listener() {
  97.             @Override
  98.             public void call(Object... args) {
  99.                 Log.info("Error getted!");
  100.             }
  101.         });
  102.         socket.on("TiaMessage", new Emitter.Listener() {
  103.             @Override
  104.             public void call(Object... args) {
  105.                 Log.info("TiaMessage!");
  106.             }
  107.         });
  108.         socket.on("GetNextCard", new Emitter.Listener() {
  109.             @Override
  110.             public void call(Object... args) {
  111.                 Log.info("GetNextCard!");
  112.             }
  113.         });
  114.         socket.on("logged_out", new Emitter.Listener() {
  115.             @Override
  116.             public void call(Object... args) {
  117.                 Log.info("logged_out!");
  118.             }
  119.         });
  120.  
  121.         socket.connect();
  122.         values.take();
  123.  
  124.         socket.disconnect();
  125.  
  126.  
  127.     }
  128.  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement