I aim to have a bayeux client that listens to the server for messages. However, I am still struggling with making the connection with server of the bayeux client. The server requires login credentials. But i found many examples without any login details. Using some examples i found that the bayeux client object needs LongPollingTransport object. which contains an httpclient. However, i want to make first a handshake so that i know the connection is working then i should start listening to the server on the channel /test/temp .The url of the server is (url = "https://manse.abcd.fi"; ) and the client definition given below.But since the Bayuex http url requires login. How can i pass the login(user and password) to establish the connection. I feel like it should be given somewhere in the LongPollingTransport class. So far i have failed to connect.
import org.cometd.bayeux.Channel;
import org.cometd.bayeux.Message;
import org.cometd.bayeux.client.ClientSessionChannel;
import org.cometd.bayeux.client.ClientSessionChannel.MessageListener;
import org.cometd.client.BayeuxClient;
import org.cometd.client.transport.ClientTransport;
import org.cometd.client.transport.LongPollingTransport;
import org.eclipse.jetty.client.ContentExchange;
import org.eclipse.jetty.client.HttpClient;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
public class Test {
public static void main(String[] args) {
HttpClient httpClient = new HttpClient();
// LongPollingTransport transport = new LongPollingTransport(options, httpClient);
String url = "https://manse.soneraiox.fi";
//LongPollingTransport transport = new LongPollingTransport(url, null, httpClient);
// String url = "https://manse.soneraiox.fi";
BayeuxClient client = new BayeuxClient(url, LongPollingTransport.create(null));
//System.out.println(client.isHandshook);
// Handshake
client.handshake();
Boolean a=client.isHandshook();
System.out.println(a);
System.out.println(client.isConnected());
This code still gives false. I am still not sure if the LongPollingTransport.create(null) is working or not. I used the same namespace you suggested. Is that oki.
import org.cometd.bayeux.Channel;
import org.cometd.bayeux.Message;
import org.cometd.bayeux.client.ClientSessionChannel;
import org.cometd.bayeux.client.ClientSessionChannel.MessageListener;
import org.cometd.client.BayeuxClient;
import org.cometd.client.transport.ClientTransport;
import org.cometd.client.transport.LongPollingTransport;
import org.eclipse.jetty.client.ContentExchange;
import org.eclipse.jetty.client.HttpClient;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
public class Stest {
public static void main(String[] args) {
HttpClient httpClient = new HttpClient();
// LongPollingTransport transport = new LongPollingTransport(options, httpClient);
String url = "https://manse.soneraiox.fi";
//LongPollingTransport transport = new LongPollingTransport(url, null, httpClient);
// String url = "https://manse.soneraiox.fi";
BayeuxClient client = new BayeuxClient(url, LongPollingTransport.create(null));
//System.out.println(client.isHandshook);
// Handshake
Map<String, Object> extra = new HashMap<>();
Map<String, Object> ext = new HashMap<>();
extra.put(Message.EXT_FIELD, ext);
Map<String, Object> authn = new HashMap<>();
ext.put("com.acme.authn", authn);
authn.put("username", "[email protected]");
authn.put("password", "xxxxx");
client.handshake(extra);
//client.handshake();
Boolean a=client.isHandshook();
System.out.println(a);
System.out.println(client.isConnected());
}
}
You may want to read the documentation about CometD authentication as a basis.
The answer to this question really depends on the authentication details.
If you manage authentication via the application (not via standard HTTP Basic/Digest), then you can pass the authentication credentials as extra fields in the handshake:
It is good practice to "namespace" extra fields, like done in the example above with the
com.acme.authnfield.Performing the authentication via CometD has the advantage that it works with any transport, either HTTP (HTTP/1.1 or HTTP/2) or WebSocket.
Alternatively, you can perform authentication via other means, and then pass the authentication tokens to CometD.
For example, typically HTTP-based authentications establish a cookie that must be passed in each subsequent request and that is validated by the server.
There are virtually infinite variations about how you can authenticate externally and then pass the authentication token to CometD, but so far CometD has been flexible enough to accommodate most if not all of them.
You don't give enough details for a precise answer, but the above should get you started.