Search code examples
javawebsocketvert.xjava-websocketvertx-httpclient

Vert.x websocket client - 400 Bad Request


How can I connect Cex.IO websocket API from my Java verticles?

The problem is that Vert.x doesn't provide me with a way to connect only with WsURI as Node.JS does. I have to specify port and host and get HTTP 400 Bad Request exception.

With Node.js you do:

var WebSocketClient = require('websocket').client;
var client = new WebSocketClient();
client.connect("wss://ws.cex.io/ws/");

With Vert.x you have to do

int host = 443; // That's defaults
String host = "cex.io"; // Am I right by specifying this host?
HttpClient client = Vertx.vertx().createHttpClient();
client.websocket(port, host, "wss://ws.cex.io/ws/", ws -> { ...});

Solution

  • This HttpClient#websocket method takes a relative URI as third parameter.

    You should be able to connect like this:

    client = vertx.createHttpClient(new HttpClientOptions()
      .setDefaultHost("ws.cex.io")
      .setDefaultPort(443)
      .setSsl(true));
    
    client.websocket("/ws", ws -> {
      // Work with the websocket
    });