2011-04-01 91 views

回答

3

有一個免費的Java API here。下面的摘錄是如何發送消息的簡單示例。

final STSession session = new STSession("SameTimeSender"); 
session.loadAllComponents(); 
session.start(); 

CommunityService comm = (CommunityService) session.getCompApi(CommunityService.COMP_NAME); 
comm.addLoginListener(new LoginListener() { 

    public void loggedOut(LoginEvent arg0) { 
    } 

    public void loggedIn(LoginEvent arg0) { 
     LookupService lookup = (LookupService) session.getCompApi(LookupService.COMP_NAME); 
     Resolver resolver = lookup.createResolver(true, false, true, false); 
     resolver.addResolveListener(new ResolveListener() { 

      public void resolved(ResolveEvent re) { 
       STUser user = (STUser) re.getResolved(); 

       InstantMessagingService imservice = (InstantMessagingService) session.getCompApi(InstantMessagingService.COMP_NAME); 

       final Im im = imservice.createIm(user, EncLevel.ENC_LEVEL_NONE, ImTypes.IM_TYPE_CHAT); 
       im.addImListener(new ImListener() { 

        public void textReceived(ImEvent arg0) { 
        } 

        public void openImFailed(ImEvent arg0) { 
        } 

        public void imOpened(ImEvent arg0) { 
         im.sendText(false, "hello world!"); 
        } 

        public void imClosed(ImEvent arg0) { 
        } 

        public void dataReceived(ImEvent arg0) { 
        } 
       }); 
       im.open(); 
      } 

      public void resolveFailed(ResolveEvent arg0) { 
      } 

      public void resolveConflict(ResolveEvent arg0) { 
      } 
     }); 

     resolver.resolve(recipientId); 
    } 
}); 

comm.loginByPassword(hostname, userId, password); 
相關問題