001/** 002 * 003 * Copyright 2021 Florian Schmaus. 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.jivesoftware.smack.full; 018 019import java.io.IOException; 020import java.net.URISyntaxException; 021import java.util.Date; 022import java.util.logging.Logger; 023 024import org.jivesoftware.smack.SmackConfiguration; 025import org.jivesoftware.smack.SmackException; 026import org.jivesoftware.smack.XMPPException; 027 028import org.jivesoftware.smack.c2s.ModularXmppClientToServerConnection; 029import org.jivesoftware.smack.c2s.ModularXmppClientToServerConnectionConfiguration; 030import org.jivesoftware.smack.debugger.ConsoleDebugger; 031import org.jivesoftware.smack.packet.Message; 032import org.jivesoftware.smack.websocket.XmppWebSocketTransportModuleDescriptor; 033import org.jivesoftware.smackx.disco.ServiceDiscoveryManager; 034 035import org.jxmpp.util.XmppDateTime; 036 037public class WebSocketConnectionTest { 038 039 static { 040 SmackConfiguration.DEBUG = true; 041 } 042 043 public static void main(String[] args) 044 throws URISyntaxException, SmackException, IOException, XMPPException, InterruptedException { 045 String jid, password, websocketEndpoint, messageTo = null; 046 if (args.length < 3 || args.length > 4) { 047 throw new IllegalArgumentException(); 048 } 049 050 jid = args[0]; 051 password = args[1]; 052 websocketEndpoint = args[2]; 053 if (args.length >= 4) { 054 messageTo = args[3]; 055 } 056 057 testWebSocketConnection(jid, password, websocketEndpoint, messageTo); 058 } 059 060 public static void testWebSocketConnection(String jid, String password, String websocketEndpoint) 061 throws URISyntaxException, SmackException, IOException, XMPPException, InterruptedException { 062 testWebSocketConnection(jid, password, websocketEndpoint, null); 063 } 064 065 public static void testWebSocketConnection(String jid, String password, String websocketEndpoint, String messageTo) 066 throws URISyntaxException, SmackException, IOException, XMPPException, InterruptedException { 067 ModularXmppClientToServerConnectionConfiguration.Builder builder = ModularXmppClientToServerConnectionConfiguration.builder(); 068 builder.removeAllModules() 069 .setXmppAddressAndPassword(jid, password) 070 .setDebuggerFactory(ConsoleDebugger.Factory.INSTANCE) 071 ; 072 073 XmppWebSocketTransportModuleDescriptor.Builder websocketBuilder = XmppWebSocketTransportModuleDescriptor.getBuilder(builder); 074 websocketBuilder.explicitlySetWebSocketEndpointAndDiscovery(websocketEndpoint, false); 075 builder.addModule(websocketBuilder.build()); 076 077 ModularXmppClientToServerConnectionConfiguration config = builder.build(); 078 ModularXmppClientToServerConnection connection = new ModularXmppClientToServerConnection(config); 079 080 connection.setReplyTimeout(5 * 60 * 1000); 081 082 connection.addConnectionStateMachineListener((event, c) -> { 083 Logger.getAnonymousLogger().info("Connection event: " + event); 084 }); 085 086 connection.connect(); 087 088 connection.login(); 089 090 if (messageTo != null) { 091 Message message = connection.getStanzaFactory().buildMessageStanza() 092 .to(messageTo) 093 .setBody("It is alive! " + XmppDateTime.formatXEP0082Date(new Date())) 094 .build() 095 ; 096 connection.sendStanza(message); 097 } 098 099 Thread.sleep(1000); 100 101 connection.disconnect(); 102 103 ModularXmppClientToServerConnection.Stats connectionStats = connection.getStats(); 104 ServiceDiscoveryManager.Stats serviceDiscoveryManagerStats = ServiceDiscoveryManager.getInstanceFor(connection).getStats(); 105 106 // CHECKSTYLE:OFF 107 System.out.println("WebSocket successfully finished, yeah!\n" + connectionStats + '\n' + serviceDiscoveryManagerStats); 108 // CHECKSTYLE:ON 109 } 110}