⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 x437.htm

📁 Its a xmpp protocol book
💻 HTM
字号:
<HTML
><HEAD
><TITLE
>A Simple Script</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.64
"><LINK
REL="HOME"
TITLE="Programming Jabber"
HREF="book1.htm"><LINK
REL="UP"
TITLE="A Taste of Things to Come"
HREF="c324.htm"><LINK
REL="PREVIOUS"
TITLE="Imaginary Conversation"
HREF="x332.htm"><LINK
REL="NEXT"
TITLE="Inside Jabber"
HREF="c445.htm"></HEAD
><BODY
CLASS="SECT1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>Programming Jabber</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="x332.htm"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 1. A Taste of Things to Come</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="c445.htm"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="JABTDG-CH-1-SECT-1.2"
>A Simple Script</A
></H1
><P
>Before moving on, let's have a look how simple it is to interact with
Jabber. <A
HREF="x437.htm#JABTDG-CH-1-EX-1"
>Example 1-1</A
> shows a simple Perl script
that connects to a Jabber server, authenticates, checks who's online,
and sends those people a reminder message. It uses the Net::Jabber 
library, which provides a high-level API to many Jabber-related functions
such as handling the connection to the server (this is via another 
library that Net::Jabber uses&mdash;XML::Stream), authentication, events,
and all the mechanisms to parse and create Jabber traffic.</P
><DIV
CLASS="EXAMPLE"
><A
NAME="JABTDG-CH-1-EX-1"
></A
><P
><B
>Example 1-1. A simple Jabber script</B
></P
><PRE
CLASS="SCREEN"
>#!/usr/bin/perl

use Net::Jabber qw(Client);
use strict;

# List of addressees for our reminder
our @addressees;

# What we want to send
my  $reminder = $ARGV[0] or die "No reminder!";

# Connect to our Jabber server
my $c= Net::Jabber::Client-&#62;new();
$c-&#62;Connect('hostname' =&#62; 'yak',
            'port'     =&#62; 5222);

# Authenticate
$c-&#62;AuthSend('username' =&#62; 'reminder',
             'password' =&#62; 'secret',
             'resource' =&#62; 'reminder');

# Set handler to deal with presence packets
# that might (will) be pushed to us (we're
# not interested in any other type of packet)
$c-&#62;SetCallBacks('presence' =&#62; \&#38;handle_presence);

# Send out our own presence, and run an
# event loop for up to 5 seconds to 
# catch any packets pushed to us
$c-&#62;PresenceSend();
$c-&#62;Process(5);

# Create a new message with our reminder text
my $m = Net::Jabber::Message-&#62;new();
$m-&#62;SetBody($reminder);

# Send the message to each of the addressees collected
# in the handle_presence() subroutine
foreach my $jid (@addressees) {

  $m-&#62;SetTo($jid);
  $c-&#62;Send($m);

}

# Disconnect from the Jabber server and exit
$c-&#62;Disconnect;
exit(0);


# Deal with presence packets
sub handle_presence {

  my ($sid, $presence) = @_;

  # Get the presence
  my $show = $presence-&#62;GetShow() || 'online';

  # If the user is around, add to addressee list
  # 'around' here means 'online' or 'chat'
  push @addressees, $presence-&#62;GetFrom()
    if $show eq 'online' or $show eq 'chat';

}</PRE
></DIV
><P
>The script is fairly self-explanatory. 
For now, we'll leave the script's description to the comments
embedded within it; by the end of the book, you should have a good
understanding of how to put together complete applications and utilities
using Jabber libraries in Perl, Python, and Java.</P
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="x332.htm"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="book1.htm"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="c445.htm"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Imaginary Conversation</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="c324.htm"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Inside Jabber</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -