Reputation: 5226
webSocketClient = new WebSocketClient();
webSocketClient.start();
wsTransport = new JettyWebSocketTransport(null, null, webSocketClient);
bayeuxClient = new BayeuxClient(url, wsTransport);
bayeuxClient.getChannel(someChannel);
bayeauxClient.handshake(handshakeAuthRequest);
Considering the above Java code works (and everything had been declared), how can I specify the Jetty ClientTransport wsTransport in Javascript (node.js)?
I have tried something along the lines of:
var connection = new comet.CometD();
connection.configure({
url: url,
channel: someChannel
}
connection.handshake(handshakeAuthRequest, function (response) {
console.log(response);
});
But I get an Unknown Bayeaux Transport reason for failure, probably because I have no Jetty transport protocol specified anywhere, nor can I find how to do it for javascript. Part of the response below.
{
id: '1',
successful: false,
channel: '/meta/handshake',
failure: {
reason: 'Unknown Bayeux Transport',
exception: undefined,
httpCode: 400,
connectionType: 'long-polling',
......
}
Upvotes: 0
Views: 408
Reputation: 5226
The short answer is that you can define the websocket protocol by calling yourCometDObject.registerTransport('websocket', new comet.WebSocketTransport())
The Long answer for my case and node.js I'll start by stating an important bits of information. Keep in mind that I am in a node.js project.
In the cometd.js file from the cometd
repo, instantiating the CometD object calls this block adding the websocket protocol:
if (window.WebSocket) {
this.registerTransport('websocket', new WebSocketTransport());
}
And here are the caveats.
cometd-nodejs-client
library, but it does not support the websocket protocol, as stated on their github page. As such, there is not way to add it later with registerTransport('websocket', new WebSocketTransport())
, since WebSocketTransport is not defined in that library.In order to use websocket in node.js
with cometd
, you have to install the browser-env
package and use it like this: require('browser-env)();
in your module. This will bypass the window object not existing part.
After that, it becomes a simple matter of getting the code I posted here and adding what's missing.
var connection = new comet.CometD();
var transport = new comet.WebSocketTransport();
connection.registerTransport('websocket', transport);
connection.unregisterTransport('callback-polling');
connection.unregisterTransport('long-polling');
connection.configure({
url: url,
channel: someChannel
}
connection.handshake(handshakeAuthRequest, function (response) {
console.log(response);
});
Notice I've also removed the long-polling and callback-polling protocols. You can also specify an index 0 for the websocket protocol, in case you want it to be the default, like:
connection.registerTransport('websocket', transport, 0);
Upvotes: 0
Reputation: 18507
If the server works with the Java client, then it should work with the JavaScript client too.
CometD has a browser-JavaScript client library that is shipped with the main CometD project and also present here.
There is also nodejs-JavaScript client library, which is a tiny wrapper around the browser-JavaScript library, here, and it's deployed as a NPM package here.
The error "Unknown Bayeux Transport" is typically due to the fact that the server does not support WebSocket, or it is incorrectly configured, so make sure it works with the Java client first if it's easier for you, and then use the NodeJS client.
Upvotes: 1