mirror of
https://github.com/vanitasvitae/Smack.git
synced 2025-09-09 00:59:39 +02:00
Rework WebSocket code
Related to SMACK-835.
This commit is contained in:
parent
0c013e4f29
commit
c5a546554b
38 changed files with 953 additions and 498 deletions
|
@ -1,6 +1,6 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2018-2020 Florian Schmaus
|
||||
* Copyright 2018-2021 Florian Schmaus
|
||||
*
|
||||
* This file is part of smack-repl.
|
||||
*
|
||||
|
@ -25,8 +25,6 @@ import java.io.IOException;
|
|||
import java.io.OutputStreamWriter;
|
||||
import java.io.PrintWriter;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Date;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
|
||||
import org.jivesoftware.smack.SmackException;
|
||||
|
@ -38,18 +36,11 @@ import org.jivesoftware.smack.compression.XMPPInputOutputStream;
|
|||
import org.jivesoftware.smack.compression.XMPPInputOutputStream.FlushMethod;
|
||||
import org.jivesoftware.smack.debugger.ConsoleDebugger;
|
||||
import org.jivesoftware.smack.debugger.SmackDebuggerFactory;
|
||||
import org.jivesoftware.smack.packet.Message;
|
||||
import org.jivesoftware.smack.sm.StreamManagementModuleDescriptor;
|
||||
import org.jivesoftware.smack.tcp.XmppTcpTransportModuleDescriptor;
|
||||
|
||||
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
|
||||
|
||||
import org.jxmpp.util.XmppDateTime;
|
||||
|
||||
public class Nio {
|
||||
|
||||
private static final Logger LOGGER = Logger.getLogger(Nio.class.getName());
|
||||
|
||||
public static void main(String[] args) throws SmackException, IOException, XMPPException, InterruptedException {
|
||||
doNio(args[0], args[1], args[2]);
|
||||
}
|
||||
|
@ -111,30 +102,7 @@ public class Nio {
|
|||
|
||||
connection.setReplyTimeout(5 * 60 * 1000);
|
||||
|
||||
connection.addConnectionStateMachineListener((event, c) -> {
|
||||
LOGGER.info("Connection event: " + event);
|
||||
});
|
||||
|
||||
connection.connect();
|
||||
|
||||
connection.login();
|
||||
|
||||
Message message = connection.getStanzaFactory().buildMessageStanza()
|
||||
.to("flo@geekplace.eu")
|
||||
.setBody("It is alive! " + XmppDateTime.formatXEP0082Date(new Date()))
|
||||
.build();
|
||||
connection.sendStanza(message);
|
||||
|
||||
Thread.sleep(1000);
|
||||
|
||||
connection.disconnect();
|
||||
|
||||
ModularXmppClientToServerConnection.Stats connectionStats = connection.getStats();
|
||||
ServiceDiscoveryManager.Stats serviceDiscoveryManagerStats = ServiceDiscoveryManager.getInstanceFor(connection).getStats();
|
||||
|
||||
// CHECKSTYLE:OFF
|
||||
System.out.println("NIO successfully finished, yeah!\n" + connectionStats + '\n' + serviceDiscoveryManagerStats);
|
||||
// CHECKSTYLE:ON
|
||||
XmppTools.modularConnectionTest(connection, "flo@geekplace.eu");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2020 Aditya Borikar
|
||||
* Copyright 2021 Florian Schmaus
|
||||
*
|
||||
* This file is part of smack-repl.
|
||||
*
|
||||
|
@ -21,7 +21,6 @@
|
|||
package org.igniterealtime.smack.smackrepl;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
import org.jivesoftware.smack.SmackException;
|
||||
|
@ -29,25 +28,51 @@ import org.jivesoftware.smack.XMPPException;
|
|||
|
||||
import org.jivesoftware.smack.c2s.ModularXmppClientToServerConnection;
|
||||
import org.jivesoftware.smack.c2s.ModularXmppClientToServerConnectionConfiguration;
|
||||
import org.jivesoftware.smack.util.TLSUtils;
|
||||
import org.jivesoftware.smack.websocket.XmppWebSocketTransportModuleDescriptor;
|
||||
|
||||
public class WebSocketConnection {
|
||||
|
||||
public static void main(String[] args) throws SmackException, IOException, XMPPException, InterruptedException, URISyntaxException {
|
||||
ModularXmppClientToServerConnectionConfiguration.Builder builder = ModularXmppClientToServerConnectionConfiguration.builder();
|
||||
builder.removeAllModules();
|
||||
builder.setXmppAddressAndPassword(args[0], args[1]);
|
||||
public static void main(String[] args)
|
||||
throws URISyntaxException, SmackException, IOException, XMPPException, InterruptedException {
|
||||
String jid, password, websocketEndpoint, messageTo = null;
|
||||
if (args.length < 3 || args.length > 4) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
jid = args[0];
|
||||
password = args[1];
|
||||
websocketEndpoint = args[2];
|
||||
if (args.length >= 4) {
|
||||
messageTo = args[3];
|
||||
}
|
||||
|
||||
TLSUtils.setDefaultTrustStoreTypeToJksIfRequired();
|
||||
|
||||
testWebSocketConnection(jid, password, websocketEndpoint, messageTo);
|
||||
}
|
||||
|
||||
public static void testWebSocketConnection(String jid, String password, String websocketEndpoint)
|
||||
throws URISyntaxException, SmackException, IOException, XMPPException, InterruptedException {
|
||||
testWebSocketConnection(jid, password, websocketEndpoint, null);
|
||||
}
|
||||
|
||||
public static void testWebSocketConnection(String jid, String password, String websocketEndpoint, String messageTo)
|
||||
throws URISyntaxException, SmackException, IOException, XMPPException, InterruptedException {
|
||||
ModularXmppClientToServerConnectionConfiguration.Builder builder = ModularXmppClientToServerConnectionConfiguration.builder();
|
||||
builder.removeAllModules()
|
||||
.setXmppAddressAndPassword(jid, password)
|
||||
;
|
||||
|
||||
// Set a fallback uri into websocket transport descriptor and add this descriptor into connection builder.
|
||||
XmppWebSocketTransportModuleDescriptor.Builder websocketBuilder = XmppWebSocketTransportModuleDescriptor.getBuilder(builder);
|
||||
websocketBuilder.explicitlySetWebSocketEndpointAndDiscovery(new URI(args[2]), false);
|
||||
websocketBuilder.explicitlySetWebSocketEndpointAndDiscovery(websocketEndpoint, false);
|
||||
builder.addModule(websocketBuilder.build());
|
||||
|
||||
ModularXmppClientToServerConnectionConfiguration config = builder.build();
|
||||
ModularXmppClientToServerConnection connection = new ModularXmppClientToServerConnection(config);
|
||||
|
||||
connection.connect();
|
||||
connection.login();
|
||||
connection.disconnect();
|
||||
connection.setReplyTimeout(5 * 60 * 1000);
|
||||
|
||||
XmppTools.modularConnectionTest(connection, messageTo);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2016 Florian Schmaus
|
||||
* Copyright 2016-2021 Florian Schmaus
|
||||
*
|
||||
* This file is part of smack-repl.
|
||||
*
|
||||
|
@ -23,6 +23,8 @@ package org.igniterealtime.smack.smackrepl;
|
|||
import java.io.IOException;
|
||||
import java.security.KeyManagementException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.Date;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.jivesoftware.smack.SmackException;
|
||||
import org.jivesoftware.smack.SmackException.NoResponseException;
|
||||
|
@ -30,15 +32,19 @@ import org.jivesoftware.smack.SmackException.NotConnectedException;
|
|||
import org.jivesoftware.smack.XMPPConnection;
|
||||
import org.jivesoftware.smack.XMPPException;
|
||||
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
|
||||
import org.jivesoftware.smack.c2s.ModularXmppClientToServerConnection;
|
||||
import org.jivesoftware.smack.packet.Message;
|
||||
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
|
||||
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
|
||||
import org.jivesoftware.smack.util.TLSUtils;
|
||||
|
||||
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
|
||||
import org.jivesoftware.smackx.iqregister.AccountManager;
|
||||
|
||||
import org.jxmpp.jid.DomainBareJid;
|
||||
import org.jxmpp.jid.impl.JidCreate;
|
||||
import org.jxmpp.jid.parts.Localpart;
|
||||
import org.jxmpp.stringprep.XmppStringprepException;
|
||||
import org.jxmpp.util.XmppDateTime;
|
||||
|
||||
public class XmppTools {
|
||||
|
||||
|
@ -106,4 +112,40 @@ public class XmppTools {
|
|||
connection.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
public static void modularConnectionTest(ModularXmppClientToServerConnection connection, String messageTo) throws XMPPException, SmackException, IOException, InterruptedException {
|
||||
connection.addConnectionStateMachineListener((event, c) -> {
|
||||
Logger.getAnonymousLogger().info("Connection event: " + event);
|
||||
});
|
||||
|
||||
connection.connect();
|
||||
|
||||
connection.login();
|
||||
|
||||
XmppTools.sendItsAlive(messageTo, connection);
|
||||
|
||||
Thread.sleep(1000);
|
||||
|
||||
connection.disconnect();
|
||||
|
||||
ModularXmppClientToServerConnection.Stats connectionStats = connection.getStats();
|
||||
ServiceDiscoveryManager.Stats serviceDiscoveryManagerStats = ServiceDiscoveryManager.getInstanceFor(connection).getStats();
|
||||
|
||||
// CHECKSTYLE:OFF
|
||||
System.out.println("NIO successfully finished, yeah!\n" + connectionStats + '\n' + serviceDiscoveryManagerStats);
|
||||
// CHECKSTYLE:ON
|
||||
}
|
||||
|
||||
public static void sendItsAlive(String to, XMPPConnection connection)
|
||||
throws XmppStringprepException, NotConnectedException, InterruptedException {
|
||||
if (to == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Message message = connection.getStanzaFactory().buildMessageStanza()
|
||||
.to(to)
|
||||
.setBody("It is alive! " + XmppDateTime.formatXEP0082Date(new Date()))
|
||||
.build();
|
||||
connection.sendStanza(message);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue