forked from knowm/XChange
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OkCoin Websocket Streaming Initial Commit
- Loading branch information
Showing
14 changed files
with
1,037 additions
and
3 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
...xamples/src/main/java/com/xeiam/xchange/examples/okcoin/streaming/OkCoinSocketIODemo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.xeiam.xchange.examples.okcoin.streaming; | ||
|
||
import java.util.Date; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import com.xeiam.xchange.Exchange; | ||
import com.xeiam.xchange.ExchangeFactory; | ||
import com.xeiam.xchange.ExchangeSpecification; | ||
import com.xeiam.xchange.dto.marketdata.Ticker; | ||
import com.xeiam.xchange.okcoin.OkCoinExchange; | ||
import com.xeiam.xchange.okcoin.service.streaming.OkCoinExchangeStreamingConfiguration; | ||
import com.xeiam.xchange.service.streaming.ExchangeEvent; | ||
import com.xeiam.xchange.service.streaming.ExchangeEventType; | ||
import com.xeiam.xchange.service.streaming.StreamingExchangeService; | ||
|
||
|
||
public class OkCoinSocketIODemo { | ||
|
||
public static void main(String[] args) throws InterruptedException { | ||
|
||
ExchangeSpecification exSpec = new ExchangeSpecification(OkCoinExchange.class); | ||
exSpec.setSecretKey("aa"); | ||
exSpec.setApiKey("bb"); | ||
|
||
Exchange exchange = ExchangeFactory.INSTANCE.createExchange(exSpec); | ||
final StreamingExchangeService service = exchange.getStreamingExchangeService(new OkCoinExchangeStreamingConfiguration()); | ||
|
||
Thread consumer = new Thread("consumer") { | ||
|
||
@Override | ||
public void run() { | ||
|
||
while (!isInterrupted()) { | ||
try { | ||
ExchangeEvent event = service.getNextEvent(); | ||
|
||
if(event != null) { | ||
System.out.println("---> " + event.getPayload() + " " + event.getEventType()); | ||
|
||
if(event.getEventType().equals(ExchangeEventType.TICKER)) { | ||
Ticker ticker = (Ticker) event.getPayload(); | ||
|
||
long x = (new Date()).getTime() - ticker.getTimestamp().getTime(); | ||
System.out.println("Delay " + x); | ||
} | ||
|
||
} | ||
|
||
} catch (InterruptedException e) { | ||
this.interrupt(); | ||
} | ||
} | ||
} | ||
|
||
}; | ||
|
||
// Start consumer. | ||
consumer.start(); | ||
|
||
// Start streaming service. | ||
service.connect(); | ||
|
||
// Demonstrate for 30 seconds. | ||
TimeUnit.SECONDS.sleep(30); | ||
|
||
// Disconnect streaming service. | ||
service.disconnect(); | ||
|
||
// Interrupt consumer to exit. | ||
consumer.interrupt(); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...e-okcoin/src/main/java/com/xeiam/xchange/okcoin/dto/marketdata/OkCoinStreamingTicker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package com.xeiam.xchange.okcoin.dto.marketdata; | ||
|
||
import java.math.BigDecimal; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
public class OkCoinStreamingTicker { | ||
|
||
private final BigDecimal high; | ||
private final BigDecimal low; | ||
private final BigDecimal buy; | ||
private final BigDecimal sell; | ||
private final BigDecimal last; | ||
private final String vol; | ||
private final long timestamp; | ||
|
||
public OkCoinStreamingTicker(@JsonProperty("high") final BigDecimal high, @JsonProperty("low") final BigDecimal low, @JsonProperty("buy") final BigDecimal buy, @JsonProperty("sell") final BigDecimal sell, | ||
@JsonProperty("last") final BigDecimal last, @JsonProperty("vol") final String vol, @JsonProperty("timestamp") long timestamp) { | ||
|
||
this.high = high; | ||
this.low = low; | ||
this.buy = buy; | ||
this.sell = sell; | ||
this.last = last; | ||
this.vol = vol; | ||
this.timestamp = timestamp; | ||
} | ||
|
||
/** | ||
* @return the high | ||
*/ | ||
public BigDecimal getHigh() { | ||
|
||
return high; | ||
} | ||
|
||
/** | ||
* @return the low | ||
*/ | ||
public BigDecimal getLow() { | ||
|
||
return low; | ||
} | ||
|
||
/** | ||
* @return the buy | ||
*/ | ||
public BigDecimal getBuy() { | ||
|
||
return buy; | ||
} | ||
|
||
/** | ||
* @return the sell | ||
*/ | ||
public BigDecimal getSell() { | ||
|
||
return sell; | ||
} | ||
|
||
/** | ||
* @return the last | ||
*/ | ||
public BigDecimal getLast() { | ||
|
||
return last; | ||
} | ||
|
||
/** | ||
* @return the vol | ||
*/ | ||
public String getVol() { | ||
|
||
return vol; | ||
} | ||
|
||
/** | ||
* @return the timestamp | ||
*/ | ||
public long getTimestamp() { | ||
return timestamp; | ||
} | ||
|
||
} |
76 changes: 76 additions & 0 deletions
76
xchange-okcoin/src/main/java/com/xeiam/xchange/okcoin/service/streaming/MD5Util.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package com.xeiam.xchange.okcoin.service.streaming; | ||
|
||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
|
||
public class MD5Util { | ||
|
||
public static String buildMysignV1(Map<String, String> sArray, | ||
String secretKey) { | ||
String mysign = ""; | ||
try { | ||
String prestr = createLinkString(sArray); | ||
prestr = prestr + "&secret_key=" + secretKey; | ||
System.out.println("prestr "+prestr); | ||
mysign = getMD5String(prestr); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
return mysign; | ||
} | ||
|
||
public static String createLinkString(Map<String, String> params) { | ||
|
||
List<String> keys = new ArrayList<String>(params.keySet()); | ||
Collections.sort(keys); | ||
String prestr = ""; | ||
for (int i = 0; i < keys.size(); i++) { | ||
String key = keys.get(i); | ||
String value = params.get(key); | ||
if (i == keys.size() - 1) { | ||
prestr = prestr + key + "=" + value; | ||
} else { | ||
prestr = prestr + key + "=" + value + "&"; | ||
} | ||
} | ||
return prestr; | ||
} | ||
|
||
private static final char HEX_DIGITS[] = { '0', '1', '2', '3', '4', '5', | ||
'6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; | ||
|
||
public static String getMD5String(String str) { | ||
try { | ||
if (str == null || str.trim().length() == 0) { | ||
return ""; | ||
} | ||
byte[] bytes = str.getBytes(); | ||
MessageDigest messageDigest = MessageDigest.getInstance("MD5"); | ||
messageDigest.update(bytes); | ||
bytes = messageDigest.digest(); | ||
StringBuilder sb = new StringBuilder(); | ||
for (int i = 0; i < bytes.length; i++) { | ||
sb.append(HEX_DIGITS[(bytes[i] & 0xf0) >> 4] + "" | ||
+ HEX_DIGITS[bytes[i] & 0xf]); | ||
} | ||
return sb.toString(); | ||
} catch (NoSuchAlgorithmException e) { | ||
e.printStackTrace(); | ||
} | ||
return ""; | ||
} | ||
|
||
public static String getParams(Map<String,String> map){ | ||
StringBuilder params = new StringBuilder("{"); | ||
for(Entry<String,String> param:map.entrySet()){ | ||
params.append("'").append(param.getKey()).append("':'").append(param.getValue()).append("',"); | ||
} | ||
params.replace(params.length()-1,params.length(),"}"); | ||
return params.toString(); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...-okcoin/src/main/java/com/xeiam/xchange/okcoin/service/streaming/OkCoinExchangeEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.xeiam.xchange.okcoin.service.streaming; | ||
|
||
import com.xeiam.xchange.service.streaming.ExchangeEvent; | ||
import com.xeiam.xchange.service.streaming.ExchangeEventType; | ||
|
||
class OkCoinExchangeEvent implements ExchangeEvent { | ||
private final String rawData; | ||
private final Object payload; | ||
private final ExchangeEventType exchangeEventType; | ||
|
||
public OkCoinExchangeEvent(ExchangeEventType exchangeEventType, Object payload) { | ||
this.rawData = null; | ||
this.payload = payload; | ||
this.exchangeEventType = exchangeEventType; | ||
} | ||
|
||
@Override | ||
public Object getPayload() { | ||
return payload; | ||
} | ||
|
||
@Override | ||
public String getData() { | ||
return rawData; | ||
} | ||
|
||
@Override | ||
public ExchangeEventType getEventType() { | ||
return exchangeEventType; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...java/com/xeiam/xchange/okcoin/service/streaming/OkCoinExchangeStreamingConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.xeiam.xchange.okcoin.service.streaming; | ||
|
||
import com.xeiam.xchange.currency.CurrencyPair; | ||
import com.xeiam.xchange.service.streaming.ExchangeStreamingConfiguration; | ||
|
||
|
||
public class OkCoinExchangeStreamingConfiguration implements ExchangeStreamingConfiguration { | ||
|
||
private final CurrencyPair[] marketDataCurrencyPairs; | ||
|
||
public OkCoinExchangeStreamingConfiguration() { | ||
marketDataCurrencyPairs = new CurrencyPair[]{ CurrencyPair.BTC_CNY }; | ||
} | ||
|
||
public OkCoinExchangeStreamingConfiguration(CurrencyPair[] marketDataCurrencyPairs) { | ||
this.marketDataCurrencyPairs = marketDataCurrencyPairs; | ||
} | ||
|
||
@Override | ||
public int getMaxReconnectAttempts() { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public int getReconnectWaitTimeInMs() { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public int getTimeoutInMs() { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public boolean isEncryptedChannel() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean keepAlive() { | ||
return false; | ||
} | ||
|
||
public CurrencyPair[] getMarketDataCurrencyPairs() { | ||
return marketDataCurrencyPairs; | ||
} | ||
} |
Oops, something went wrong.