Skip to content

Commit 6e79bb4

Browse files
雷蕊菡雷蕊菡
authored andcommitted
initial commit
1 parent 48d0ed5 commit 6e79bb4

File tree

14 files changed

+7217
-1
lines changed

14 files changed

+7217
-1
lines changed

.DS_Store

6 KB
Binary file not shown.

Java/.DS_Store

6 KB
Binary file not shown.

Java/pom.xml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.huobi.api</groupId>
6+
<artifactId>sample</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
9+
<properties>
10+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
11+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
12+
<maven.compiler.source>1.8</maven.compiler.source>
13+
<maven.compiler.target>1.8</maven.compiler.target>
14+
<java.version>1.8</java.version>
15+
16+
<jackson.version>2.8.7</jackson.version>
17+
<okhttp.version>3.6.0</okhttp.version>
18+
<slf4j.version>1.7.24</slf4j.version>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>com.fasterxml.jackson.core</groupId>
24+
<artifactId>jackson-databind</artifactId>
25+
<version>${jackson.version}</version>
26+
</dependency>
27+
<dependency>
28+
<groupId>com.squareup.okhttp3</groupId>
29+
<artifactId>okhttp</artifactId>
30+
<version>${okhttp.version}</version>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.slf4j</groupId>
34+
<artifactId>slf4j-api</artifactId>
35+
<version>${slf4j.version}</version>
36+
</dependency>
37+
38+
<dependency>
39+
<groupId>org.java-websocket</groupId>
40+
<artifactId>Java-WebSocket</artifactId>
41+
<version>1.3.0</version>
42+
</dependency>
43+
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
44+
<dependency>
45+
<groupId>com.alibaba</groupId>
46+
<artifactId>fastjson</artifactId>
47+
<version>1.2.33</version>
48+
</dependency>
49+
</dependencies>
50+
</project>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.huobi.api.market;
2+
3+
import java.io.ByteArrayInputStream;
4+
import java.io.ByteArrayOutputStream;
5+
import java.io.IOException;
6+
import java.nio.ByteBuffer;
7+
import java.nio.CharBuffer;
8+
import java.nio.charset.Charset;
9+
import java.nio.charset.CharsetDecoder;
10+
import java.util.zip.GZIPInputStream;
11+
import java.util.zip.GZIPOutputStream;
12+
13+
public class CommonUtils {
14+
15+
// buffer 转String
16+
public static String byteBufferToString(ByteBuffer buffer) {
17+
CharBuffer charBuffer = null;
18+
try {
19+
Charset charset = Charset.forName("ISO-8859-1");
20+
CharsetDecoder decoder = charset.newDecoder();
21+
charBuffer = decoder.decode(buffer);
22+
buffer.flip();
23+
return charBuffer.toString();
24+
} catch (Exception ex) {
25+
ex.printStackTrace();
26+
return null;
27+
}
28+
}
29+
30+
// 压缩
31+
public static String compress(String str) throws IOException {
32+
if (str == null || str.length() == 0) {
33+
return str;
34+
}
35+
ByteArrayOutputStream out = new ByteArrayOutputStream();
36+
GZIPOutputStream gzip = new GZIPOutputStream(out);
37+
gzip.write(str.getBytes());
38+
gzip.close();
39+
return out.toString("ISO-8859-1");
40+
}
41+
42+
// 解压缩
43+
public static String uncompress(String str) throws IOException {
44+
if (str == null || str.length() == 0) {
45+
return str;
46+
}
47+
ByteArrayOutputStream out = new ByteArrayOutputStream();
48+
ByteArrayInputStream in = new ByteArrayInputStream(str.getBytes("ISO-8859-1"));
49+
GZIPInputStream gunzip = new GZIPInputStream(in);
50+
byte[] buffer = new byte[256];
51+
int n;
52+
while ((n = gunzip.read(buffer)) >= 0) {
53+
out.write(buffer, 0, n);
54+
}
55+
return out.toString();
56+
}
57+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.huobi.api.market;
2+
3+
public class Main {
4+
public static void main(String[] args) {
5+
try {
6+
WebSocketUtils.executeWebSocket();
7+
} catch (Exception e) {
8+
e.printStackTrace();
9+
}
10+
}
11+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.huobi.api.market;
2+
3+
public class ReqModel {
4+
private String req;
5+
private Long id;
6+
7+
public String getReq() {
8+
return req;
9+
}
10+
11+
public void setReq(String req) {
12+
this.req = req;
13+
}
14+
15+
public Long getId() {
16+
return id;
17+
}
18+
19+
public void setId(Long id) {
20+
this.id = id;
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.huobi.api.market;
2+
3+
public class SubModel {
4+
private String sub;
5+
private Long id;
6+
7+
public String getSub() {
8+
return sub;
9+
}
10+
11+
public void setSub(String sub) {
12+
this.sub = sub;
13+
}
14+
15+
public Long getId() {
16+
return id;
17+
}
18+
19+
public void setId(Long id) {
20+
this.id = id;
21+
}
22+
}
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
package com.huobi.api.market;
2+
3+
import java.io.IOException;
4+
import java.net.URI;
5+
import java.nio.ByteBuffer;
6+
import java.security.cert.CertificateException;
7+
import java.security.cert.X509Certificate;
8+
import java.util.HashMap;
9+
import java.util.Map;
10+
11+
import javax.net.ssl.SSLContext;
12+
import javax.net.ssl.TrustManager;
13+
import javax.net.ssl.X509TrustManager;
14+
15+
import org.java_websocket.client.DefaultSSLWebSocketClientFactory;
16+
import org.java_websocket.client.WebSocketClient;
17+
import org.java_websocket.drafts.Draft;
18+
import org.java_websocket.drafts.Draft_17;
19+
import org.java_websocket.framing.Framedata;
20+
import org.java_websocket.handshake.ServerHandshake;
21+
22+
import com.alibaba.fastjson.JSONObject;
23+
24+
public class WebSocketUtils extends WebSocketClient {
25+
26+
// private static final String url = "wss://be.huobi.com/ws";
27+
private static final String url = "wss://api.huobi.com/ws";
28+
29+
private static WebSocketUtils chatclient = null;
30+
31+
public WebSocketUtils(URI serverUri, Draft draft) {
32+
super(serverUri, draft);
33+
}
34+
35+
public WebSocketUtils(URI serverURI) {
36+
super(serverURI);
37+
}
38+
39+
public WebSocketUtils(URI serverUri, Map<String, String> headers, int connecttimeout) {
40+
super(serverUri, new Draft_17(), headers, connecttimeout);
41+
}
42+
43+
@Override
44+
public void onOpen(ServerHandshake handshakedata) {
45+
System.out.println("开流--opened connection");
46+
}
47+
48+
@Override
49+
public void onMessage(ByteBuffer socketBuffer) {
50+
try {
51+
String marketStr = CommonUtils.byteBufferToString(socketBuffer);
52+
String market = CommonUtils.uncompress(marketStr);
53+
if (market.contains("ping")) {
54+
System.out.println(market.replace("ping", "pong"));
55+
// Client 心跳
56+
chatclient.send(market.replace("ping", "pong"));
57+
} else {
58+
System.out.println(" market:" + market);
59+
60+
}
61+
} catch (IOException e) {
62+
e.printStackTrace();
63+
}
64+
}
65+
66+
@Override
67+
public void onMessage(String message) {
68+
System.out.println("接收--received: " + message);
69+
}
70+
71+
public void onFragment(Framedata fragment) {
72+
System.out.println("片段--received fragment: " + new String(fragment.getPayloadData().array()));
73+
}
74+
75+
@Override
76+
public void onClose(int code, String reason, boolean remote) {
77+
System.out.println("关流--Connection closed by " + (remote ? "remote peer" : "us"));
78+
}
79+
80+
@Override
81+
public void onError(Exception ex) {
82+
System.out.println("WebSocket 连接异常: " + ex);
83+
}
84+
85+
public static Map<String, String> getWebSocketHeaders() throws IOException {
86+
Map<String, String> headers = new HashMap<String, String>();
87+
return headers;
88+
}
89+
90+
private static void trustAllHosts(WebSocketUtils appClient) {
91+
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
92+
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
93+
return new java.security.cert.X509Certificate[] {};
94+
}
95+
96+
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
97+
}
98+
99+
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
100+
}
101+
} };
102+
103+
try {
104+
SSLContext sc = SSLContext.getInstance("TLS");
105+
sc.init(null, trustAllCerts, new java.security.SecureRandom());
106+
appClient.setWebSocketFactory(new DefaultSSLWebSocketClientFactory(sc));
107+
} catch (Exception e) {
108+
e.printStackTrace();
109+
}
110+
}
111+
112+
public static void executeWebSocket() throws Exception {
113+
// WebSocketImpl.DEBUG = true;
114+
chatclient = new WebSocketUtils(new URI(url), getWebSocketHeaders(), 1000);
115+
trustAllHosts(chatclient);
116+
chatclient.connectBlocking();
117+
// 订阅K线数据 sub 根据自己需要订阅数据
118+
SubModel subModel = new SubModel();
119+
subModel.setSub("market.btccny.kline.1min");
120+
subModel.setId(10000L);
121+
chatclient.send(JSONObject.toJSONString(subModel));
122+
123+
// 订阅数据深度
124+
SubModel subModel1 = new SubModel();
125+
subModel1.setSub("market.btccny.depth.percent10");
126+
subModel1.setId(10001L);
127+
chatclient.send(JSONObject.toJSONString(subModel1));
128+
// 取消订阅省略
129+
130+
// 请求数据 sub 根据自己需要请求数据
131+
ReqModel reqModel = new ReqModel();
132+
reqModel.setReq("market.btccny.depth.percent10");
133+
reqModel.setId(10002L);
134+
chatclient.send(JSONObject.toJSONString(reqModel));
135+
136+
// 请求数据
137+
ReqModel reqModel1 = new ReqModel();
138+
reqModel1.setReq("market.btccny.detail");
139+
reqModel1.setId(10003L);
140+
chatclient.send(JSONObject.toJSONString(reqModel1));
141+
System.out.println("send : " + JSONObject.toJSONString(reqModel));
142+
}
143+
}

Javascript/.DS_Store

6 KB
Binary file not shown.

Javascript/index.html

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Title</title>
6+
<script src="lib/pako.js"></script>
7+
<script src="lib/jquery.min.js"></script>
8+
</head>
9+
<body>
10+
11+
<div>
12+
当前价格:<span id="nowPrice">0</span>
13+
<br>
14+
量:<span id="nowAmount">0</span>
15+
<br>
16+
涨幅:<span id="zhangfu">0</span>
17+
</div>
18+
19+
20+
<script>
21+
22+
var webSocket = new WebSocket("wss://be.huobi.com/ws");
23+
webSocket.binaryType = "arraybuffer";
24+
webSocket.onopen = function(event){
25+
console.log(("webSocket connect at time: "+new Date()));
26+
webSocket.send(JSON.stringify({'sub': 'market.ethcny.kline.1day','id': 'depth ' + new Date()}));
27+
}
28+
webSocket.onmessage = function(event){
29+
var raw_data = event.data;
30+
window.raw_data = raw_data;
31+
var ua = new Uint8Array(raw_data);
32+
var json = pako.inflate(ua,{to:"string"});
33+
var data = JSON.parse(json);
34+
if(data["ping"]){
35+
webSocket.send(JSON.stringify({"pong":data["ping"]}));
36+
}
37+
else{
38+
console.log(data);
39+
40+
if(data.ch){
41+
var tick = data.tick;
42+
$("#nowPrice").html(data.tick.close);
43+
$("#nowAmount").html(data.tick.amount+tick.count);
44+
$("#zhangfu").html((Math.floor((tick.close-tick.open)/tick.open*100*100)/100)+"%")
45+
}
46+
}
47+
}
48+
49+
webSocket.onclose = function(){
50+
console.log("webSocket connect is closed");
51+
console.log(arguments);
52+
}
53+
54+
webSocket.onerror = function(){
55+
console.log("error");
56+
console.log(arguments);
57+
}
58+
59+
</script>
60+
61+
62+
</body>
63+
</html>

Javascript/lib/jquery.min.js

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)