Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 22 additions & 23 deletions bitget-java-sdk-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
<url>http://www.bitget.com/</url>

<properties>
<java.version>1.8</java.version>
<lombok-version>1.16.20</lombok-version>
<java.version>17</java.version>
<lombok-version>1.18.30</lombok-version>
<okhttp-version>4.7.2</okhttp-version>
<retorfit2-version>2.8.1</retorfit2-version>
<fastjson-version>1.2.70</fastjson-version>
<fastjson-version>1.2.80</fastjson-version>
<commons-lang3-version>3.7</commons-lang3-version>
<commons-codec-version>1.14</commons-codec-version>
<junit-version>4.12</junit-version>
Expand Down Expand Up @@ -107,8 +107,8 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
Expand All @@ -118,6 +118,21 @@
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>

</plugins>
</build>

Expand All @@ -129,26 +144,10 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
<version>3.11.0</version>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ private OkHttpClient httpClient() {
.addInterceptor(new HttpStatusInterceptor())
.connectTimeout(parameter.getTimeout(), TimeUnit.SECONDS)
.readTimeout(parameter.getTimeout(), TimeUnit.SECONDS)
.proxy(parameter.getProxy())
.build();
}

Expand Down Expand Up @@ -107,6 +108,10 @@ public Response intercept(Chain chain) {
.addHeader(HttpHeader.ACCESS_TIMESTAMP, timestamp)
.url(url);

if (clientParameter.isPapTrading()) {
requestBuilder.addHeader(HttpHeader.PAPTRADING, "1");
}

Request request = requestBuilder.build();
return chain.proceed(request);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ public class HttpHeader {
public static final String CONTENT_TYPE = "Content-Type";
public static final String LOCALE = "locale";
public static final String COOKIE = "Cookie";
public static final String PAPTRADING = "paptrading";
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import lombok.Data;
import lombok.NoArgsConstructor;

import java.net.Proxy;

/**
* @author bitget-sdk-team
* @date 2019-01-15
Expand Down Expand Up @@ -50,4 +52,15 @@ public class ClientParameter {
* 签名类型
*/
private SignTypeEnum signType = SignTypeEnum.SHA256;


/**
* 代理配置
*/
private Proxy proxy;

/**
* 是否是模拟交易
*/
private boolean isPapTrading = false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@
import com.bitget.openapi.dto.request.ws.WsBaseReq;
import com.bitget.openapi.dto.request.ws.WsLoginReq;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;

import java.math.BigDecimal;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.time.Instant;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
Expand All @@ -26,6 +29,7 @@
import java.util.stream.Collectors;
import java.util.zip.CRC32;

@Slf4j
public class BitgetWsHandle implements BitgetWsClient {
public static final String WS_OP_LOGIN = "login";
public static final String WS_OP_SUBSCRIBE = "subscribe";
Expand All @@ -47,15 +51,12 @@ private BitgetWsHandle(BitgetClientBuilder builder) {
webSocket = initClient();
}

private static void printLog(String msg, String type) {
System.out.println("[" + DateUtil.getUnixTime() + "] [" + type.toUpperCase() + "] " + msg);
}

private WebSocket initClient() {
OkHttpClient client = new OkHttpClient.Builder()
.writeTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.connectTimeout(60, TimeUnit.SECONDS)
.proxy(builder.proxy)
.build();

Request request = new Request.Builder()
Expand All @@ -67,7 +68,7 @@ private WebSocket initClient() {
if (builder.isLogin) {
login();
}
printLog("start connect ....", "info");
log.info("开始连接WebSocket服务器...");
while (!connectStatus) {
}

Expand All @@ -80,13 +81,11 @@ public static BitgetClientBuilder builder() {

@Override
public void sendMessage(WsBaseReq<?> req) {
printLog("send message:" + JSONObject.toJSONString(req), "info");
sendMessage(JSONObject.toJSONString(req));
}

@Override
public void sendMessage(String message) {
printLog("start send message:" + message, "INFO");
webSocket.send(message);
}

Expand Down Expand Up @@ -130,7 +129,7 @@ public void login() {
List<WsLoginReq> args = buildArgs();
sendMessage(new WsBaseReq<>(WS_OP_LOGIN, args));
//休眠1s,等待登录结果
printLog("login in ......", "info");
log.info("开始WebSocket登录流程...");
while (!this.loginStatus) {
try {
Thread.sleep(10000);
Expand All @@ -140,7 +139,7 @@ public void login() {
e.printStackTrace();
}
}
printLog("login in ......end", "info");
log.info("WebSocket登录成功");
}

private List<WsLoginReq> buildArgs() {
Expand Down Expand Up @@ -212,7 +211,7 @@ public void onOpen(final WebSocket webSocket, final Response response) {

@Override
public void onClosing(WebSocket webSocket, int code, String reason) {
System.out.println("Connection is about to disconnect!");
log.info("连接即将断开!");
close();
if (!reconnectStatus) {
reConnect();
Expand All @@ -222,7 +221,7 @@ public void onClosing(WebSocket webSocket, int code, String reason) {

@Override
public void onClosed(final WebSocket webSocket, final int code, final String reason) {
System.out.println("Connection dropped!" + reason);
log.info("连接已断开:{}", reason);
close();
if (!reconnectStatus) {
reConnect();
Expand All @@ -249,20 +248,20 @@ public void onFailure(final WebSocket webSocket, final Throwable t, final Respon
public void onMessage(final WebSocket webSocket, final String message) {
try {
if (message.equals("pong")) {
printLog("Keep connected:" + message, "info");
log.info("保持连接:收到pong响应");
return;
}
JSONObject jsonObject = JSONObject.parseObject(message);
if (jsonObject.containsKey("code") && !jsonObject.get("code").toString().equals("0")) {
printLog("code not is 0 msg:" + message, "error");
log.error("code not is 0 msg:{}", message);
if (Objects.nonNull(builder.errorListener)) {
builder.errorListener.onReceive(message);
}
return;
}

if (jsonObject.containsKey("event") && jsonObject.get("event").equals("login")) {
printLog("login msg:" + message, "info");
log.info("login msg:{}", message);
loginStatus = true;
return;
}
Expand All @@ -285,9 +284,9 @@ public void onMessage(final WebSocket webSocket, final String message) {
return;
}
}
printLog("receive op msg:" + message, "info");
log.info("receive op msg:{}", message);
} catch (Exception e) {
printLog("receive error msg:" + message, "error");
log.error("receive error msg:{}", message);
}
}

Expand Down Expand Up @@ -353,7 +352,7 @@ private void close() {

private void reConnect() {
reconnectStatus = true;
printLog("start reconnection ...", "info");
log.info("开始重新连接WebSocket服务器...");
initClient();
if (CollectionUtils.isNotEmpty(allSuribe)) {
subscribe(new ArrayList<>(allSuribe));
Expand All @@ -374,6 +373,8 @@ public static class BitgetClientBuilder {
private SubscriptionListener listener;
private SubscriptionListener errorListener;

private Proxy proxy;

public BitgetClientBuilder listener(SubscriptionListener listener) {
this.listener = listener;
return this;
Expand Down Expand Up @@ -414,6 +415,23 @@ public BitgetClientBuilder signType(SignTypeEnum signType) {
return this;
}

public BitgetClientBuilder proxy(Proxy proxy) {
this.proxy = proxy;
return this;
}

/**
* 设置代理
* @param type 代理类型
* @param hostname 主机名
* @param port 端口号
* @return
*/
public BitgetClientBuilder proxy(Proxy.Type type,String hostname, int port) {
this.proxy = new Proxy(type, new InetSocketAddress(hostname, port));
return this;
}

public BitgetWsClient build() {
return new BitgetWsHandle(this);
}
Expand All @@ -432,9 +450,9 @@ public BookInfo() {

public BookInfo merge(BookInfo updateInfo) {
this.asks = merge(this.asks, updateInfo.getAsks(), false);
printLog("asks sort uniq:" + JSONObject.toJSONString(this.asks), "info");
log.info("asks sort uniq:{}", JSONObject.toJSONString(this.asks));
this.bids = merge(this.bids, updateInfo.getBids(), true);
printLog("bids sort uniq:" + JSONObject.toJSONString(this.bids), "info");
log.info("bids sort uniq:{}", JSONObject.toJSONString(this.bids));
return this;
}

Expand Down Expand Up @@ -484,8 +502,8 @@ public boolean checkSum(int checkSum, int gear) {
CRC32 crc32 = new CRC32();
crc32.update(str.getBytes());
int value = (int) crc32.getValue();
printLog("check val:" + str, "info");
printLog("start checknum mergeVal:" + value + ",checkVal:" + checkSum, "info");
log.info("check val:{}", str);
log.info("start checknum mergeVal:{} checkVal:{}", value, checkSum);
return value == checkSum;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
package com.bitget.openapi.ws;

import com.alibaba.fastjson.JSONObject;
import com.bitget.openapi.common.enums.SignTypeEnum;
import com.bitget.openapi.dto.request.ws.SubscribeReq;

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.ArrayList;
import java.util.List;

public class BitgetWsClientTest {

public static final String PUSH_URL = "wss://ws.bitget.com/mix/v1/stream";
public static final String PUSH_URL = "wss://wspap.bitget.com/mix/v1/stream";
public static final String API_KEY = "";
public static final String SECRET_KEY = "";
public static final String PASS_PHRASE = "";

public static final String PROXY_HOST = "localhost";
public static final int PROXY_PORT = 10808;
public static void main(String[] args) {
BitgetWsClient client = BitgetWsHandle.builder()
.pushUrl(PUSH_URL)
.apiKey(API_KEY)
.secretKey(SECRET_KEY)
.passPhrase(PASS_PHRASE)
.proxy(Proxy.Type.HTTP,PROXY_HOST, PROXY_PORT)
// .signType(SignTypeEnum.RSA)
.isLogin(true)
//默认监听处理,如订阅时指定监听,默认不再接收该channel订阅信息
Expand All @@ -32,7 +37,8 @@ public static void main(String[] args) {
}).build();

List<SubscribeReq> list = new ArrayList<SubscribeReq>() {{
add(SubscribeReq.builder().instType("UMCBL").channel("positions").instId("default").build());
// add(SubscribeReq.builder().instType("UMCBL").channel("positions").instId("default").build());
add(SubscribeReq.builder().instType("mc").channel("ticker").instId("ETHUSDT").build());
// add(SubscribeReq.builder().instType("SP").channel("candle1W").instId("BTCUSDT").build());
}};
client.subscribe(list);
Expand Down