2

after sending custom element i get disconnected.Is there a way? my code for connection

  [self addDelegate:self delegateQueue:dispatch_get_main_queue()];

[self setHostName:@"bowerchat.com"];
[self setHostPort:5222];

self.myJID = [XMPPJID jidWithString:[NSString stringWithFormat:@"%@@bowerchat.com",UD_getObj(@"myPhoneNumber")]];

NSError * error;
[self connectWithTimeout:300 error:&error];

disconnected issue

Domain=GCDAsyncSocketErrorDomain Code=7 "Socket closed by remote peer" 

After found problem i changed @"type" parameter as @"type2" and it is working now...but i cannot find why?

-(void)sendMessageToServer:(NSDictionary*)paraDict{
NSString * userPhone = [[DBHelper sharedObject]getUserPhone:paraDict[@"friend_id"]];
NSXMLElement *a = [NSXMLElement elementWithName:@"request" xmlns:@"urn:xmpp:receipts"];
if(paraDict[@"type"] != nil){
NSMutableDictionary * k = [[NSMutableDictionary alloc]initWithDictionary:paraDict];

    NSString * typeValue = [k[@"type"]copy];
    [k removeObjectForKey:@"type"];
    [k setObject:typeValue forKey:@"type2"];

paraDict = k;
}
XMPPElement *e = [[XMPPElement alloc] initWithName:@"message"];
for(NSString * key in paraDict.allKeys){
    [e addAttributeWithName:key stringValue:paraDict[key]];
}
[e addAttributeWithName:@"to" stringValue:getJabberString(userPhone)];
[e addAttributeWithName:@"from" stringValue:getMyJabberString];
[e addChild:a];
NSLog(@"%@",[e attributesAsDictionary]);
[self sendElement:e];

}

  • You are not showing any custom element in this code. You are not even showing anything relating to server or connection, just formatting a JID. Could you please expand / clarify your question ? – Mickaël Rémond Aug 03 '15 at 17:21
  • 1
    i found problem.the problem is attirube name of type.if there is attribute name "type" the error occurs.why? –  Aug 03 '15 at 17:27
  • Please, update your question to show the XMPP packet causing issue. This is needed to get a meaningful reply. – Mickaël Rémond Aug 03 '15 at 17:28
  • updated.@MickaëlRémond –  Aug 03 '15 at 18:26

2 Answers2

2

The message type attribute is build into the XMPP protocol. It has a precise meaning on XMPP and you cannot put what you want as the value of the type message attribute. As defined in XMPP RFC, the only possible types are:

  • chat
  • error
  • groupchat
  • headline
  • normal

You cannot put anything in message type, or indeed, otherwise the server should disconnect you.

Reference: http://xmpp.org/rfcs/rfc6121.html#message-syntax-type

Mickaël Rémond
  • 9,035
  • 1
  • 24
  • 44
0

Here's a good guideline on how to add custom data to your stanzas:

Do not:

  • Add new attributes on the <message>, <iq> or <presence> elements.
  • Create new type values for stanzas.
  • Create new top-level elements.
  • Put your custom data in the <body> of a <message>.
  • Invent new values for the <show> element in <presence> stanzas (allowed values are listed in RFC 6121 section 4.7.2.1)

Do:

Add a new XML element in your own namespace:

<message
   from='romeo@montague.net/orchard'
   to='juliet@capulet.com/balcony'
   type='chat'>
   <data xmlns='https://example.im/my-awesome-new-xmpp-app'></data>
</message>
xnyhps
  • 3,306
  • 17
  • 21