Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multi-chat rooms handling #103

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 37 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,46 @@ XmppDemo uses a Flux approach (check its `XmppStore`) to communicate with a samp
## Example

```js
var XMPP = require('react-native-xmpp');
var XMPP = require("react-native-xmpp");

// optional callbacks
XMPP.on('message', (message) => console.log('MESSAGE:' + JSON.stringify(message)));
XMPP.on('iq', (message) => console.log('IQ:' + JSON.stringify(message)));
XMPP.on('presence', (message) => console.log('PRESENCE:' + JSON.stringify(message)));
XMPP.on('error', (message) => console.log('ERROR:' + message));
XMPP.on('loginError', (message) => console.log('LOGIN ERROR:' + message));
XMPP.on('login', (message) => console.log('LOGGED!'));
XMPP.on('connect', (message) => console.log('CONNECTED!'));
XMPP.on('disconnect', (message) => console.log('DISCONNECTED!'));
XMPP.on("message", message =>
console.log("MESSAGE:" + JSON.stringify(message))
);
XMPP.on("iq", message => console.log("IQ:" + JSON.stringify(message)));
XMPP.on("presence", message =>
console.log("PRESENCE:" + JSON.stringify(message))
);
XMPP.on("error", message => console.log("ERROR:" + message));
XMPP.on("loginError", message => console.log("LOGIN ERROR:" + message));
XMPP.on("login", message => console.log("LOGGED!"));
XMPP.on("connect", message => console.log("CONNECTED!"));
XMPP.on("disconnect", message => console.log("DISCONNECTED!"));

// trustHosts (ignore self-signed SSL issues)
// Warning: Do not use this in production (security will be compromised).
XMPP.trustHosts(['chat.google.com']);
XMPP.trustHosts(["chat.google.com"]);

// connect
XMPP.connect(MYJID, MYPASSWORD);
XMPP.connect(
MYJID,
MYPASSWORD
);

// send message
XMPP.message('Hello world!', TOJID);
XMPP.message("Hello world!", TOJID);

// join room(s)
XMPP.joinRoom(ROOMJID_1, ROOMNICKNAME);
XMPP.joinRoom(ROOMJID_2, ROOMNICKNAME);

// send message to room(s)
XMPP.sendRoomMessage(ROOMJID_1, "Hello room 1!");
XMPP.sendRoomMessage(ROOMJID_2, "Hello room 2!");

// leave room(s)
XMPP.leaveRoom(ROOMJID_1);
XMPP.leaveRoom(ROOMJID_2);

// disconnect
XMPP.disconnect();
Expand All @@ -51,21 +70,21 @@ XMPP.removeListener(TYPE);

### iOS

Please use CocoaPods
Please use CocoaPods

2. Install latest XMPPFramework:
https://github.com/robbiehanson/XMPPFramework
`pod 'XMPPFramework', :git => 'https://github.com/robbiehanson/XMPPFramework.git', :branch => 'master'`
https://github.com/robbiehanson/XMPPFramework
`pod 'XMPPFramework', :git => 'https://github.com/robbiehanson/XMPPFramework.git', :branch => 'master'`

3. Add this package pod:
`pod 'RNXMPP', :path => '../node_modules/react-native-xmpp'`
`pod 'RNXMPP', :path => '../node_modules/react-native-xmpp'`

If you have problems with latest 4.0 XMPPFramework and/or XCode 9.3, you may use old one with forked KissXML:
`pod 'XMPPFramework', '~> 3.7.0'`
`pod 'KissXML', :git => "https://github.com/aksonov/KissXML.git", :branch => '5.1.4'`


### Android

`react-native link react-native-xmpp`

If it doesn't link the react-native-xmpp correct:
Expand All @@ -91,7 +110,7 @@ dependencies {
On top, where imports are:

```java
import rnxmpp.RNXMPPPackage;
import com.rnxmpp.RNXMPPPackage;
```

Add the `ReactVideoPackage` class to your list of exported packages.
Expand Down
24 changes: 24 additions & 0 deletions RNXMPP/RNXMPP.m
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,25 @@ -(void)onLogin:(NSString *)username password:(NSString *)password {
[[RNXMPPService sharedInstance] sendStanza:stanza];
}

RCT_EXPORT_METHOD(joinRoom:(NSString *)roomJID nickName:(NSString *)nickname)
{
[RNXMPPService sharedInstance].delegate = self;
[[RNXMPPService sharedInstance] joinRoom:roomJID nickName:nickname];
}

RCT_EXPORT_METHOD(leaveRoom:(NSString *)roomJID)
{
[RNXMPPService sharedInstance].delegate = self;
[[RNXMPPService sharedInstance] leaveRoom:roomJID];
}

RCT_EXPORT_METHOD(sendRoomMessage:(NSString *)roomJID message:(NSString *)message)
{
[RNXMPPService sharedInstance].delegate = self;
[[RNXMPPService sharedInstance] sendRoomMessage:roomJID message:message];
}


- (NSDictionary *)constantsToExport
{
return @{ PLAIN_AUTH : @(Plain),
Expand All @@ -163,5 +182,10 @@ - (NSDictionary *)constantsToExport
};
};

+ (BOOL)requiresMainQueueSetup
{
return NO;
}


@end
10 changes: 8 additions & 2 deletions RNXMPP/RNXMPPService.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
@interface RNXMPPService : NSObject
{
XMPPStream *xmppStream;
XMPPRoom *xmppRoom;
XMPPRoster *xmppRoster;
XMPPRosterMemoryStorage *xmppRosterStorage;
XMPPReconnect *xmppReconnect;
Expand All @@ -54,6 +55,8 @@
@property (nonatomic, strong, readonly) XMPPStream *xmppStream;
@property (nonatomic, strong, readonly) XMPPReconnect *xmppReconnect;
@property (nonatomic, weak) id<RNXMPPServiceDelegate> delegate;
@property (nonatomic) XMPPRoom *xmppRoom;
@property (nonatomic) NSMutableDictionary *xmppRooms;

+(RNXMPPService *) sharedInstance;
- (void)trustHosts:(NSArray *)hosts;
Expand All @@ -63,8 +66,11 @@
- (void)sendMessage:(NSString *)text to:(NSString *)username thread:(NSString *)thread;
- (void)sendPresence:(NSString *)to type:(NSString *)type;
- (void)removeRoster:(NSString *)to;
-(void)fetchRoster;
-(void)sendStanza:(NSString *)stanza;
- (void)fetchRoster;
- (void)sendStanza:(NSString *)stanza;
- (void)joinRoom:(NSString *)roomJID nickName:(NSString *)nickname;
- (void)sendRoomMessage:(NSString *)roomJID message:(NSString *)message;
- (void)leaveRoom:(NSString *)roomJID;

@end

36 changes: 36 additions & 0 deletions RNXMPP/RNXMPPService.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#import "XMPPLogging.h"
#import "XMPPReconnect.h"
#import "XMPPUser.h"
#import "XMPPRoom.h"
#import "XMPPRoomMemoryStorage.h"
#import <CocoaLumberjack/DDLog.h>
#import "DDTTYLogger.h"
#import <CFNetwork/CFNetwork.h>
Expand Down Expand Up @@ -36,6 +38,8 @@ @implementation RNXMPPService

@synthesize xmppStream;
@synthesize xmppReconnect;
@synthesize xmppRoom;
@synthesize xmppRooms;

+(RNXMPPService *) sharedInstance {
static RNXMPPService *sharedInstance = nil;
Expand Down Expand Up @@ -190,6 +194,9 @@ - (void)setupStream

// You may need to alter these settings depending on the server you're connecting to
customCertEvaluation = YES;

// init xmppRooms dict
xmppRooms = [NSMutableDictionary dictionary];
}

- (void)teardownStream
Expand Down Expand Up @@ -263,6 +270,8 @@ - (BOOL)connect:(NSString *)myJID withPassword:(NSString *)myPassword auth:(Auth
if (myJID == nil || myPassword == nil) {
return NO;
}

NSLog(@"Connect using JID %@", myJID);

[xmppStream setMyJID:[XMPPJID jidWithString:myJID]];
username = myJID;
Expand Down Expand Up @@ -591,4 +600,31 @@ -(void)fetchRoster {
[xmppRoster fetchRoster];
}

-(void)joinRoom:(NSString *)roomJID nickName:(NSString *)nickname{
XMPPJID *ROOM_JID = [XMPPJID jidWithString:roomJID];
XMPPRoomMemoryStorage *roomMemoryStorage = [[XMPPRoomMemoryStorage alloc] init];
xmppRoom = [[XMPPRoom alloc] initWithRoomStorage:roomMemoryStorage jid:ROOM_JID dispatchQueue:dispatch_get_main_queue()];
[xmppRooms setObject:xmppRoom forKey:roomJID];
NSXMLElement *history = [NSXMLElement elementWithName:@"history"];
[history addAttributeWithName:@"maxstanzas" stringValue:@"0"];
[xmppRoom activate:xmppStream];
[xmppRoom addDelegate:self delegateQueue:dispatch_get_main_queue()];
[xmppRoom joinRoomUsingNickname:nickname history:history password:nil];
}

- (void)sendRoomMessage:(NSString *)roomJID message:(NSString *)message{
if (!isXmppConnected){
[self.delegate onError:[NSError errorWithDomain:@"xmpp" code:0 userInfo:@{NSLocalizedDescriptionKey: @"Server is not connected, please reconnect"}]];
return;
}
[[xmppRooms objectForKey:roomJID] sendMessageWithBody:message];
}

-(void)leaveRoom:(NSString *)roomJID{
[[xmppRooms objectForKey:roomJID] leaveRoom];
[[xmppRooms objectForKey:roomJID] deactivate];
[[xmppRooms objectForKey:roomJID] removeDelegate:self];
[xmppRooms removeObjectForKey:roomJID];
}

@end
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.xmppdemo;

import com.facebook.react.ReactActivity;
import rnxmpp.RNXMPPPackage;
import com.rnxmpp.RNXMPPPackage;

public class MainActivity extends ReactActivity {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import java.util.Arrays;
import java.util.List;

import rnxmpp.RNXMPPPackage;
import com.rnxmpp.RNXMPPPackage;

public class MainApplication extends Application implements ReactApplication {

Expand Down
10 changes: 5 additions & 5 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ repositories {

dependencies {
compile "com.facebook.react:react-native:+"
compile "org.igniterealtime.smack:smack-android:4.1.0"
compile "org.igniterealtime.smack:smack-android:4.1.9"
// Optional for XMPPTCPConnection
compile "org.igniterealtime.smack:smack-tcp:4.1.0"
compile "org.igniterealtime.smack:smack-tcp:4.1.9"
// Optional for XMPP-IM (RFC 6121) support (Roster, Threaded Chats, …)
compile "org.igniterealtime.smack:smack-im:4.1.0"
compile "org.igniterealtime.smack:smack-im:4.1.9"
// Optional for XMPP extensions support
compile "org.igniterealtime.smack:smack-extensions:4.1.0"
compile "org.igniterealtime.smack:smack-extensions:4.1.9"
// Support SCRAM-SHA-1
compile "org.igniterealtime.smack:smack-sasl-provided:4.1.0"
compile "org.igniterealtime.smack:smack-sasl-provided:4.1.9"
}
2 changes: 1 addition & 1 deletion android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="rnxmpp">
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.rnxmpp">

</manifest>
26 changes: 22 additions & 4 deletions android/src/main/java/rnxmpp/RNXMPPModule.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
package rnxmpp;
package com.rnxmpp;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableArray;

import org.jivesoftware.smackx.muc.MultiUserChat;
import org.jivesoftware.smackx.muc.MultiUserChatManager;

import java.util.logging.Logger;

import rnxmpp.service.RNXMPPCommunicationBridge;
import rnxmpp.service.XmppServiceSmackImpl;
import com.rnxmpp.service.RNXMPPCommunicationBridge;
import com.rnxmpp.service.XmppServiceSmackImpl;

/**
* Created by Kristian Frølund on 7/19/16.
* Copyright (c) 2016. Teletronics. All rights reserved
*/
public class RNXMPPModule extends ReactContextBaseJavaModule implements rnxmpp.service.XmppService {
public class RNXMPPModule extends ReactContextBaseJavaModule implements com.rnxmpp.service.XmppService {

public static final String MODULE_NAME = "RNXMPP";
Logger logger = Logger.getLogger(RNXMPPModule.class.getName());
Expand Down Expand Up @@ -42,6 +45,21 @@ public void connect(String jid, String password, String authMethod, String hostn
this.xmppService.connect(jid, password, authMethod, hostname, port);
}

@ReactMethod
public void joinRoom(String mucJid, String userNickname) {
this.xmppService.joinRoom(mucJid, userNickname);
}

@ReactMethod
public void sendRoomMessage(String mucJid, String text) {
this.xmppService.sendRoomMessage(mucJid, text);
}

@ReactMethod
public void leaveRoom(String mucJid) {
this.xmppService.leaveRoom(mucJid);
}

@Override
@ReactMethod
public void message(String text, String to, String thread){
Expand Down
2 changes: 1 addition & 1 deletion android/src/main/java/rnxmpp/RNXMPPPackage.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package rnxmpp;
package com.rnxmpp;

import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.JavaScriptModule;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package rnxmpp.service;
package com.rnxmpp.service;

import android.support.annotation.Nullable;

Expand All @@ -15,7 +15,9 @@
import org.jivesoftware.smack.roster.RosterEntry;
import org.jivesoftware.smack.roster.RosterGroup;

import rnxmpp.utils.Parser;
import com.rnxmpp.utils.Parser;



/**
* Created by Kristian Frølund on 7/19/16.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.rnxmpp.service;


public interface XmppGroupMessageListener {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.rnxmpp.service;

import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.MessageListener;

import java.util.logging.Level;
import java.util.logging.Logger;

public class XmppGroupMessageListenerImpl implements XmppGroupMessageListener, MessageListener {

XmppServiceListener xmppServiceListener;
Logger logger;

public XmppGroupMessageListenerImpl(XmppServiceListener xmppServiceListener, Logger logger) {
this.xmppServiceListener = xmppServiceListener;
this.logger = logger;
}

public void processMessage(Message message) {
this.xmppServiceListener.onMessage(message);
logger.log(Level.INFO, "Received a new group message", message.toString());
}

}


Loading