-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
96 lines (78 loc) · 2.33 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
'use strict';
var React = require('react-native');
var {NativeAppEventEmitter, NativeModules} = React;
var RNXMPP = NativeModules.RNXMPP;
var map = {
'message' : 'RNXMPPMessage',
'iq': 'RNXMPPIQ',
'presence': 'RNXMPPPresence',
'connect': 'RNXMPPConnect',
'disconnect': 'RNXMPPDisconnect',
'error': 'RNXMPPError',
'loginError': 'RNXMPPLoginError',
'login': 'RNXMPPLogin',
'roster': 'RNXMPPRoster'
}
class XMPP {
constructor(){
this.PLAIN = 0;
this.SCRAMSHA1 = 1;
this.DigestMD5 = 2;
this.isConnected = false;
this.isLogged = false;
// NativeAppEventEmitter.addListener(map.connect, this.onConnected.bind(this));
// NativeAppEventEmitter.addListener(map.disconnect, this.onDisconnected.bind(this));
// NativeAppEventEmitter.addListener(map.error, this.onError.bind(this));
// NativeAppEventEmitter.addListener(map.loginError, this.onLoginError.bind(this));
// NativeAppEventEmitter.addListener(map.login, this.onLogin.bind(this));
}
onConnected(){
console.log("Connected");
this.isConnected = true;
}
onLogin(){
console.log("Logged");
this.isLogged = true;
}
onDisconnected(error){
console.log("Disconnected, error"+error);
this.isConnected = false;
this.isLogged = false;
}
onError(text){
console.log("Error: "+text);
}
onLoginError(text){
this.isLogged = false;
console.log("LoginError: "+text);
}
on(type, callback){
if (map[type]){
NativeAppEventEmitter.addListener(
map[type],callback);
} else {
console.error("No registered type: "+type);
}
}
connect(username, password, host, auth = this.SCRAMSHA1){
React.NativeModules.RNXMPP.connect(username, password, host, auth);
}
message(text, user){
React.NativeModules.RNXMPP.message(text, user);
}
fetchRoster(){
RNXMPP.fetchRoster();
}
presence(to, type){
React.NativeModules.RNXMPP.presence(to, type);
}
removeFromRoster(to){
React.NativeModules.RNXMPP.removeRoster(to);
}
disconnect(){
if (this.isConnected){
React.NativeModules.RNXMPP.disconnect();
}
}
}
module.exports = new XMPP();