XMPP and xmpp protocols

Source: Internet
Author: User

XMPP and xmpp protocols
Logon and logout

# Import "AppDelegate. h "# import" XMPPFramework. h "/** log on to AppDelegate. initialize XMPPStream 2. connect to the server [upload a JID] 3. after successfully connecting to the service, send a password authorization 4. after successful authorization, send an "online" Message */@ interface AppDelegate () <XMPPStreamDelegate> {XMPPStream * _ xmppStream;} // 1. initialize XMPPStream-(void) setupXMPPStream; // 2. connect to the server-(void) connectToHost; // 3. after successfully connecting to the service, send the password authorization-(void) sendPwdToHost; // 4. after successful authorization, send the "online" message-(void) sendOnlineToHost; @ end @ implementation App Delegate-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {// when the program is started, it connects to the host [self connectToHost]; return YES ;} # pragma mark-Private method # pragma mark initializes XMPPStream-(void) setupXMPPStream {_ xmppStream = [[XMPPStream alloc] init]; // sets the proxy [_ xmppStream addDelegate: self resume: dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)] ;}# pragma Mark connect to the server-(void) connectToHost {NSLog (@ "start to connect to the server"); if (! _ XmppStream) {[self setupXMPPStream];} // sets the login user's JID // resource to identify the user's login client iphone android XMPPJID * myJID = [XMPPJID jidWithUser: @ "zhouhao" domain: @ "wangzhaolu. local "resource: @" iphone "]; _ xmppStream. myJID = myJID; // set the server domain name _ xmppStream. hostName = @ "wangzhaolu. local "; // not only the domain name, but also the IP address // You can omit _ xmppStream if the server port is 5222. hostPort = 5222; // connect NSError * err = nil; if (! [_ XmppStream connectWithTimeout: XMPPStreamTimeoutNone error: & err]) {NSLog (@ "% @", err) ;}# after The pragma mark is successfully connected to the service, resend password authorization-(void) sendPwdToHost {NSLog (@ "resend password authorization"); NSError * err = nil; [_ xmppStream authenticateWithPassword: @ "123456" error: & err]; if (err) {NSLog (@ "% @", err) ;}# after successful pragma mark authorization, send the "online" message-(void) sendOnlineToHost {NSLog (@ "send online message"); XMPPPresence * presence = [XMPPPresence presence]; NSLog (@ "% @", presence); [_ xmppStream sendElement: presence] ;}# pragma mark-XMPPStream proxy # pragma mark is successfully connected to the host-(void) xmppStreamDidConnect :( XMPPStream *) sender {NSLog (@ ""); // After the host connection is successful, send the password to authorize [self sendPwdToHost];} # pragma mark disconnects from the host-(void) xmppStreamDidDisconnect :( XMPPStream *) sender withError :( NSError *) error {// if an error occurs, it indicates that the connection fails. NSLog (@ "disconnect from the host % @", error);} # successful pragma mark authorization-(void) xmppStreamDidAuthenticate :( XMPPStream *) sender {NSLog (@ "authorized"); [self sendOnlineToHost] ;}# pragma mark failed to authorize-(void) xmppStream :( XMPPStream *) sender didNotAuthenticate :( DDXMLElement *) error {NSLog (@ "Authorization failed % @", error) ;}# pragma mark-public method-(void) logout {// 1. "Send" offline "message" XMPPPresence * offline = [XMPPPresence presenceWithType: @ "unavailable"]; [_ xmppStream sendElement: offline]; // 2. disconnect from the server [_ xmppStream disconnect];} @ end
Openfire after login

Openfire after cancellation
# Import "ViewController. h "# import" AppDelegate. h "@ interface ViewController () @ end @ implementation ViewController-(void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event {// deregister AppDelegate * app = [UIApplication sharedApplication]. delegate; [app logout];} @ end

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.