Java
Chilkat2 Java
WebSocket Binance Trade Stream (subscribe and receive updates)
See more WebSocket Examples
Subscribe to a binance trade stream and receive updates.Chilkat Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.Rest;
import com.chilkatsoft.chilkat2.WebSocket;
public class ChilkatExample {
public static void main(String[] args) {
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
WebSocket ws = new WebSocket();
// For brevity, this example does not check for errors when etablishing the WebSocket connection.
// See Establish WebSocket Connection for more complete sample code for making the connection.
Rest rest = new Rest();
// Connect to wss://stream.binance.com:9443
String responseBody;
try {
rest.connect("stream.binance.com", 9443, true, false);
ws.useConnection(rest);
ws.addClientHeaders();
// Raw streams are accessed at /ws/<streamName>
responseBody = rest.fullRequestNoBody("GET", "/ws/btcusdt");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
try {
ws.validateServerHandshake();
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
System.out.println(responseBody);
System.out.println(rest.getResponseHeader());
return;
}
System.out.println(responseBody);
System.out.println(rest.getResponseHeader());
// POST JSON to subscribe to a stream
// {
// "method": "SUBSCRIBE",
// "params":
// [
// "btcusdt@aggTrade",
// "btcusdt@depth"
// ],
// "id": 1
// }
JsonObject json = new JsonObject();
json.updateString("method", "SUBSCRIBE");
json.updateString("params[0]", "btcusdt@aggTrade");
json.updateString("params[1]", "btcusdt@depth");
json.updateInt("id", 1);
// Send a full message in a single frame
boolean finalFrame = true;
try {
ws.sendFrame(json.emit(), finalFrame);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
JsonObject jsonTradeData = new JsonObject();
jsonTradeData.setEmitCompact(false);
// Begin reading the trade stream response.
// We'll just read the 1st 10 updates and then exit..
int numTradesReceived = 0;
while (numTradesReceived < 5) {
try {
ws.readFrame();
} catch (ChilkatException e) {
System.out.println("Failed to receive a frame");
System.out.println("ReadFrame fail reason = " + ws.getReadFrameFailReason());
System.out.println(e.getLastErrorText());
return;
}
// The responses we desire are in Text frames, where the opcode = 1.
if (ws.getFrameOpcodeInt() == 1) {
String receivedJson = ws.getFrameData();
jsonTradeData.load(receivedJson);
System.out.println(jsonTradeData.emit());
numTradesReceived = numTradesReceived + 1;
}
}
// Close the websocket connection.
try {
ws.sendClose(true, 1000, "Closing this websocket.");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// Read the Close response.
try {
ws.readFrame();
} catch (ChilkatException e) {
System.out.println("ReadFrame fail reason = " + ws.getReadFrameFailReason());
System.out.println(e.getLastErrorText());
return;
}
System.out.println("Success.");
// The output of the above code is shown here:
}
}