IOS XMPP 프로토콜 기반 개발 - XMPPFramewok 프레임워크(二): 서버 연결
5371 단어 frame
서버에 연결하기 전에 다음 사항을 준비해야 합니다.
1. XMPP 서버 구축
2. 서버 주소 및 포트 설정
[_xmppStream setHostName:@"127.0.0.1"];
[_xmppStream setHostPort:5222];
3. connect 호출
주요 항목:
JID - 형식은 사용자 이름 + @ + 서버 주소여야 합니다. 예:[email protected]
연결 서버는 JID 설정을 해야 합니다. 계정이 없으면 임의의 값을 설정할 수 있습니다.
구체적인 코드는 다음과 같습니다.
- (BOOL)connect:(NSString*)user withpassword:(NSString*)pwd
{
if (user != nil) {
user = [NSString stringWithFormat:@"%@@%@",user,_xmppStream.hostName];
}
if (![_xmppStream isDisconnected]) {
if(_isLogined){
NSError *error = nil;
password=pwd;
[[self xmppStream] setMyJID:[XMPPJID jidWithString:user resource:@"ios"]];
if (![[self xmppStream] authenticateWithPassword:password error:&error])
{
NSLog(@"Error authenticating: %@", error);
}
}
return YES;
}
NSString *myJID = user;
NSString *myPassword = pwd;
if ( myPassword != nil) {
password = myPassword;
}
if (myJID != nil) {
[_xmppStream setMyJID:[XMPPJID jidWithString:myJID resource:@"ios"]];
}else{
[_xmppStream setMyJID:[XMPPJID jidWithString:_xmppStream.hostName resource:@"ios"]];
}
NSError *error = nil;
[_xmppStream connectWithTimeout:10 error:&error];
if(error)
{
NSLog(@" %@",error);
}
return YES;
}
반환된 콜백 결과 연결
///
- (void)xmppStreamDidConnect:(XMPPStream *)sender
{
NSLog(@" ");
isXmppConnected = YES;
if(_isLogined){
NSError *error = nil;
if (![[self xmppStream] authenticateWithPassword:password error:&error])
{
NSLog(@"Error authenticating: %@", error);
}
}
}
///
- (void)xmppStreamDidDisconnect:(XMPPStream *)sender withError:(NSError *)error
{
NSLog(@" ");
if (!isXmppConnected)
{
//DDLogError(@"Unable to connect to server. Check xmppStream.hostName");
}
}
연결 해제
[_xmppStream disconnect];
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Django의 클래스 뷰 및 중간부품Django의 클래스 뷰 및 중간부품 클래스 보기 클래스 1의 도입 이해하기 편함그러나 하나의 보기에 대응하는 경로가 다양한 HTTP 요청 방식을 지원할 때 하나의 함수에서 서로 다른 업무 논리를 작성해야 하기 때문...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.