package chaz.trade.input.okcoin;

import com.google.gson.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.websocket.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.zip.GZIPInputStream;

/**
 * Created by Administrator on 2017/8/31.
 */
@ClientEndpoint
public class EndPoint {
    private static final Logger LOGGER = LoggerFactory.getLogger(EndPoint.class);

    @OnOpen
    public void onOpen(Session session) {
        LOGGER.info("Connected to server: " + session.getBasicRemote());
        try {
            SubscribeRequestModel model = new SubscribeRequestModel();
            model.setEvent("addChannel");
            model.setChannel("ok_sub_spotcny_btc_depth_20");
            Gson gson = new GsonBuilder().create();
            session.getBasicRemote().sendText(gson.toJson(model));
        } catch (IOException ex) {
            LOGGER.error("got exception", ex);
        }
    }

    @OnMessage
    public void processMessage(String message) {
        JsonElement jelement = new JsonParser().parse(message);
        JsonObject jobject = jelement.getAsJsonObject();
        if (jobject.has("channel")) {
            if (StringUtils.equals(jobject.get("channel").getAsString(),"ok_sub_spotcny_btc_depth_20")) {
                LOGGER.info("subscribe huibi market scueess");
            }
        }
    }

    @OnMessage
    public void processBinary(byte[] bytes) throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayInputStream in = new ByteArrayInputStream(new String(bytes, StandardCharsets.ISO_8859_1).getBytes("ISO-8859-1"));
        GZIPInputStream gunzip = new GZIPInputStream(in);
        byte[] buffer = new byte[256];
        int n;
        while ((n = gunzip.read(buffer)) >= 0) {
            out.write(buffer, 0, n);
        }
        LOGGER.info("received bytes {}",out.toString());
    }

    @OnError
    public void processError(Throwable t) {
        LOGGER.error("got error from server", t);
    }
}

@@ -1132,24 +1132,76 @@
  message) %7B%0A
+        LOGGER.info(%22received message:%22+message);%0A//
         Json
@@ -1252,16 +1252,18 @@
 ssage);%0A
+//
         
@@ -1307,24 +1307,26 @@
 onObject();%0A
+//
         if (
@@ -1347,24 +1347,26 @@
 hannel%22)) %7B%0A
+//
             
@@ -1455,24 +1455,26 @@
 pth_20%22)) %7B%0A
+//
             
@@ -1528,513 +1528,14 @@
 %22);%0A
+//
     
-        %7D%0A        %7D%0A    %7D%0A%0A    @OnMessage%0A    public void processBinary(byte%5B%5D bytes) throws IOException %7B%0A        ByteArrayOutputStream out = new ByteArrayOutputStream();%0A        ByteArrayInputStream in = new ByteArrayInputStream(new String(bytes, StandardCharsets.ISO_8859_1).getBytes(%22ISO-8859-1%22));%0A        GZIPInputStream gunzip = new GZIPInputStream(in);%0A        byte%5B%5D buffer = new byte%5B256%5D;%0A        int n;%0A        while ((n = gunzip.read(buffer)) %3E= 0) %7B%0A            out.write(buffer, 0, n);%0A
     
@@ -1540,24 +1540,26 @@
       %7D%0A
+//
         
 LOGGER.i
@@ -1554,56 +1554,9 @@
     
-LOGGER.info(%22received bytes %7B%7D%22,out.toString());
+%7D
 %0A   

remove bytes processor