项目作者: Coditation

项目描述 :
XMPP module for React Native
高级语言: JavaScript
项目地址: git://github.com/Coditation/reactnative-xmpp.git
创建时间: 2016-07-18T20:55:23Z
项目社区:https://github.com/Coditation/reactnative-xmpp

开源协议:Other

下载


ReactNative-XMPP

XMPP module for React Native

This module is a work in progress and is a barebones XMPP client module at the moment. It should be enough to get some functional XMPP implementation in React Native started. Our development team intends to continue contributing towards further development of the module in coming days and months. Following is a very high level and a draft roadmap for the lib -

  • Add unit tests
  • Support SASL and Digest-MD5 based Auth. The lib/module currently supports plain auth.
  • Implement handling of use-cases such as MUC Invitation, Subscription IQ etc.
  • Fork/branch to implement new template driven design
  • Achieve feature complete wrt core XMPP spec and XMPP MUC specs

Using ReactNative-XMPP

  • Install rn-nodeify globally (https://github.com/mvayngrib/rn-nodeify) npm install -g rn-nodeify
  • Install reactnative-xmpp: npm install https://github.com/Coditation/reactnative-xmpp
  • Run rn-nodeify --install buffer,events,process,stream,util,inherits,fs,path --hack
  • If you face Sockets Undefined issue somewhere in react-native-socket, do following: Open XCode, remove libTcpSockets.a and re-add it. Run rnpm link react-native-tcp

Sample Code

  1. import React, { Component } from 'react';
  2. import {
  3. AppRegistry,
  4. StyleSheet,
  5. Text,
  6. View
  7. } from 'react-native';
  8. global.Buffer = global.Buffer || require('buffer').Buffer
  9. var xmpp = require('reactnative-xmpp');
  10. // Use canonicalHost as the host name on the Jabber server and when you need to connect to any host
  11. // other than localhost
  12. var conf = {login: 'user2',
  13. password: 'Chelsea@100',
  14. domain: 'localhost',
  15. host: 'localhost',
  16. canonicalHost: 'localhost'};
  17. var client = new xmpp.Client(conf);
  18. client.on('ready', function () {
  19. setTimeout(function () {
  20. for(var i = 0; i < client.roster.length; i++) {
  21. console.log(client.roster[i].getAttribute('jid'));
  22. if(client.roster[i].getAttribute('jid') === 'user3@localhost') {
  23. client.write('<presence to="'+ client.roster[i].getAttribute('jid') +'" type="subscribe" ></presence>');
  24. client.subscribe(client.roster[i].getAttribute('jid'), 'Friends', null, function(elt) {
  25. console.log("Subscribe response: " + elt);
  26. })
  27. }
  28. }
  29. }, 1000);
  30. });
  31. client.on('presence.subscribe',function(element) {
  32. client.write('<presence to="'+ element.getAttribute('from') +'" type="subscribed" ></presence>');
  33. });
  34. client.on('ready', function() {
  35. client.join('testroom@conference.localhost', 'chetans', function(elem) {
  36. console.log(elem)
  37. });
  38. })
  39. class reactnativexmpp extends Component {
  40. render() {
  41. return (
  42. <View style={styles.container}>
  43. <Text style={styles.welcome}>
  44. Welcome to React Native!
  45. </Text>
  46. <Text style={styles.instructions}>
  47. To get started, edit index.ios.js
  48. </Text>
  49. <Text style={styles.instructions}>
  50. Press Cmd+R to reload,{'\n'}
  51. Cmd+D or shake for dev menu
  52. </Text>
  53. </View>
  54. );
  55. }
  56. }
  57. const styles = StyleSheet.create({
  58. container: {
  59. flex: 1,
  60. justifyContent: 'center',
  61. alignItems: 'center',
  62. backgroundColor: '#F5FCFF',
  63. },
  64. welcome: {
  65. fontSize: 20,
  66. textAlign: 'center',
  67. margin: 10,
  68. },
  69. instructions: {
  70. textAlign: 'center',
  71. color: '#333333',
  72. marginBottom: 5,
  73. },
  74. });
  75. AppRegistry.registerComponent('reactnativexmpp', () => reactnativexmpp);