建立自己的IM与GTALK通信
来源:优易学  2010-1-15 12:05:25   【优易学:中国教育考试门户网】   资料下载   IT书店
  自己的上网环境需要代理,上网处理smack代理的资源没有是正常的,只有寥寥几个记载,并且是设置java环境的网络代理,设置了在smack上还是不行的。后面实在不行了,看看api,原来本身就有支持的废话小讲直接贴代码
  view plaincopy to clipboardprint?
  public class Test {
  public static void main(String[] args) throws XMPPException, IOException{
  XMPPConnection.DEBUG_ENABLED = true;
  ConnectionConfiguration config = new ConnectionConfiguration("talk.google.com",5222,"gmail.com");
  ProxyInfo proxy = ProxyInfo.forHttpProxy("代理地址", 8080, null, null);//代理服务器设置 这里用http代理的 具体看网络环境
  //使用新的socket工厂
  config.setSocketFactory(proxy.getSocketFactory());
  XMPPConnection conn = new XMPPConnection(config);
  conn.connect();
  conn.login("xxxx@gmail.com", "xxxxx");
  Chat chat = conn.getChatManager().createChat("xxxxxxxx@gmail.com", new MessageListener(){
  //接收信息
  @Override
  public void processMessage(Chat chat, Message message) {
  System.out.println(message.getFrom() + ":" + message.getBody());
  }
  });
  //监听键盘输入 发送信息
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  String line = null;
  while((line = br.readLine()) != null){
  if(line.equals(":q")){
  break;
  }
  chat.sendMessage(line);
  }
  conn.disconnect();
  System.exit(0);
  }
  }
  public class Test {
  public static void main(String[] args) throws XMPPException, IOException{
  XMPPConnection.DEBUG_ENABLED = true;
  ConnectionConfiguration config = new ConnectionConfiguration("talk.google.com",5222,"gmail.com");
  ProxyInfo proxy = ProxyInfo.forHttpProxy("代理地址", 8080, null, null);//代理服务器设置 这里用http代理的 具体看网络环境
  //使用新的socket工厂
  config.setSocketFactory(proxy.getSocketFactory());
  XMPPConnection conn = new XMPPConnection(config);
  conn.connect();
  conn.login("xxxx@gmail.com", "xxxxx");
  Chat chat = conn.getChatManager().createChat("xxxxxxxx@gmail.com", new MessageListener(){
  //接收信息
  @Override
  public void processMessage(Chat chat, Message message) {
  System.out.println(message.getFrom() + ":" + message.getBody());
  }
  });
  //监听键盘输入 发送信息
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  String line = null;
  while((line = br.readLine()) != null){
  if(line.equals(":q")){
  break;
  }
  chat.sendMessage(line);
  }
  conn.disconnect();
  System.exit(0);
  }
  }

责任编辑:cyth

文章搜索:
 相关文章
热点资讯
资讯快报
热门课程培训