基于RethinkDB +React Native开发实时移动Web应用程序

译文
开发 后端
一个实时应用程序能够使用户第一时间了解他想了解的信息。用户不必不停地刷新用户界面来获取最新的消息更新,应用程序的服务器端会自动更新客户端应用的。在本文中,我们将使用时下流行的RethinkDB +React Native框架开发一个真正实时的移动Web应用程序。

简介

一个实时应用程序能够使用户***时间了解他想了解的信息。用户不必不停地刷新用户界面来获取***的消息更新,应用程序的服务器端会自动更新客户端应用的。在本文中,我们将使用时下流行的RethinkDB +React Native框架开发一个真正实时的移动Web应用程序。

注意,阅读本文的前提是假定你已经了解了有关React Native编程的基础知识,因此,示例程序中有些相关的细节代码在此并不想赘述。如果你还是一名初学者,那么建议你先读一下这个网址处的文章:https://www.sitepoint.com/build-android-app-react-native/。如果你想继续阅读本文的话,建议你首先下载本文的示例工程源码,地址是https://github.com/sitepoint-editors/rn-rethinkdb-socketio-newssharer

下图给出示例工程的运行时快照。

下面,让我们首先来分析一下手机应用程序的编码情况,然后再来讨论服务器端组件相关编程,其中将使用到Node、Express、Socket.io和RethinkDB等技术。

安装依赖性

从你克隆下来的工程中导航到NewsShare目录下,然后执行命令npm install来安装一下下面这些工程依赖项:

1.react-native:这是React Native(本机)框架。

2.lodash:用于管理新闻项数组,以便通过票数来限制和排序该数组。

3.react-native-modalbox:用于创建模态对话框来共享一则新闻。

4.react-native-button:React Native模态对话框依赖于它,用于创建按钮。

5.react-native-vector-icons:用于使用流行图标集,如FontAwesome和Ionicons等来创建图标。这主要用于为投票按钮创建图标。

6.socket.io-client:Socket.io的客户端组件,它是一个实时应用程序框架。

链接图标

安装依赖关系后,还需要一个额外步骤就是使图标正常工作,即要将它们链接到应用程序。这是通过使用rnpm——React Native的软件包管理器实现的。

我们要使用npm来安装 rnpm,格式如下:

npm install rnpm -g

然后,就可以执行NewsSharer目录下的rnpm link命令来链接图标了。

开发移动客户端程序

下面给出的是文件index.android.js的内容:

  1. import React, { Component } from 'react'
  2.  
  3. import { 
  4.  
  5. AppRegistry, 
  6.  
  7. StyleSheet, 
  8.  
  9. View 
  10.  
  11. from 'react-native'
  12.  
  13. import Main from './components/Main'
  14.  
  15. class NewsSharer extends Component { 
  16.  
  17. render() { 
  18.  
  19. return ( 
  20.  
  21. <View style={styles.container}> 
  22.  
  23. <Main /> 
  24.  
  25. </View
  26.  
  27. ); 
  28.  
  29.  
  30.  
  31. const styles = StyleSheet.create({ 
  32.  
  33. container: { 
  34.  
  35. flex: 1, 
  36.  
  37. justifyContent: 'center'
  38.  
  39. alignItems: 'center'
  40.  
  41. backgroundColor: '#F5FCFF'
  42.  
  43.  
  44. }); 
  45.  
  46. AppRegistry.registerComponent('NewsSharer', () => NewsSharer); 

该文件是Android应用程序的入口点文件。如果你想把它部署到iOS上,那么你可以把上述代码复制到文件index.ios.js中。

此文件的主要任务是导入Main组件,组件是应用程序的核心所在。当您导入组件而不是重复地为每个平台编码时,这可以大大减少编程的重复性。

编写主应用程序组件

在路径components/Main.js下创建文件Main.js,内容如下:

  1. import React, { Component } from 'react'
  2.  
  3. import { 
  4.  
  5. AppRegistry, 
  6.  
  7. StyleSheet, 
  8.  
  9. Text, 
  10.  
  11. View
  12.  
  13. TextInput, 
  14.  
  15. TouchableHighlight, 
  16.  
  17. Linking, 
  18.  
  19. ListView 
  20.  
  21. from 'react-native'
  22.  
  23. import Button from 'react-native-button'
  24.  
  25. import Modal from 'react-native-modalbox'
  26.  
  27. import Icon from 'react-native-vector-icons/Octicons'
  28.  
  29. import "../UserAgent"
  30.  
  31. import io from 'socket.io-client/socket.io'
  32.  
  33. import _ from 'lodash'
  34.  
  35. var base_url = 'http://YOUR_DOMAIN_NAME_OR_IP_ADDRESS:3000'
  36.  
  37. export default class Main extends Component { 
  38.  
  39. constructor(props){ 
  40.  
  41. super(props); 
  42.  
  43. this.socket = io(base_url, { 
  44.  
  45. transports: ['websocket'
  46.  
  47. }); 
  48.  
  49. this.state = { 
  50.  
  51. is_modal_open: false
  52.  
  53. news_title: ''
  54.  
  55. news_url: ''
  56.  
  57. news_items_datasource: new ListView.DataSource({ 
  58.  
  59. rowHasChanged: (row1, row2) => row1 !== row2, 
  60.  
  61. }), 
  62.  
  63. is_news_loaded: false
  64.  
  65. news: {}, 
  66.  
  67. news_items: [] 
  68.  
  69. }; 
  70.  
  71.  
  72. getNewsItems(){ 
  73.  
  74. fetch(base_url + '/news'
  75.  
  76. .then((response) => { 
  77.  
  78. return response.json(); 
  79.  
  80. }) 
  81.  
  82. .then((news_items) => { 
  83.  
  84. this.setState({ 
  85.  
  86. 'news_items': news_items 
  87.  
  88. }); 
  89.  
  90. var news_datasource = this.state.news_items_datasource.cloneWithRows(news_items); 
  91.  
  92. this.setState({ 
  93.  
  94. 'news': news_datasource, 
  95.  
  96. 'is_news_loaded'true 
  97.  
  98. }); 
  99.  
  100. return news_items; 
  101.  
  102. }) 
  103.  
  104. .catch((error) => { 
  105.  
  106. alert('Error occured while fetching news items'); 
  107.  
  108. }); 
  109.  
  110.  
  111. componentWillMount(){ 
  112.  
  113. this.socket.on('news_updated', (data) => { 
  114.  
  115. var news_items = this.state.news_items; 
  116.  
  117. if(data.old_val === null){ 
  118.  
  119. news_items.push(data.new_val); 
  120.  
  121. }else
  122.  
  123. _.map(news_items, function(row, index){ 
  124.  
  125. if(row.id == data.new_val.id){ 
  126.  
  127. news_items[index].upvotes = data.new_val.upvotes; 
  128.  
  129.  
  130. }); 
  131.  
  132.  
  133. this.updateUI(news_items); 
  134.  
  135. }); 
  136.  
  137.  
  138. updateUI(news_items){ 
  139.  
  140. var ordered_news_items = _.orderBy(news_items, 'upvotes''desc'); 
  141.  
  142. var limited_news_items = _.slice(ordered_news_items, 0, 30); 
  143.  
  144. var news_datasource = this.state.news_items_datasource.cloneWithRows(limited_news_items); 
  145.  
  146. this.setState({ 
  147.  
  148. 'news': news_datasource, 
  149.  
  150. 'is_news_loaded'true
  151.  
  152. 'is_modal_open'false
  153.  
  154. 'news_items': limited_news_items 
  155.  
  156. }); 
  157.  
  158.  
  159. componentDidMount(){ 
  160.  
  161. this.getNewsItems(); 
  162.  
  163.  
  164. upvoteNewsItem(id, upvotes){ 
  165.  
  166. fetch(base_url + '/upvote-newsitem', { 
  167.  
  168. method: 'POST'
  169.  
  170. headers: { 
  171.  
  172. 'Accept''application/json'
  173.  
  174. 'Content-Type''application/json'
  175.  
  176. }, 
  177.  
  178. body: JSON.stringify({ 
  179.  
  180. news_id: id, 
  181.  
  182. upvotes: upvotes + 1 
  183.  
  184. }) 
  185.  
  186. }) 
  187.  
  188. .catch((err) => { 
  189.  
  190. alert('Error occured while trying to upvote'); 
  191.  
  192. }); 
  193.  
  194.  
  195. openModal(){ 
  196.  
  197. this.setState({ 
  198.  
  199. is_modal_open: true 
  200.  
  201. }); 
  202.  
  203.  
  204. closeModal(){ 
  205.  
  206. this.setState({ 
  207.  
  208. is_modal_open: false 
  209.  
  210. }); 
  211.  
  212.  
  213. shareNews(){ 
  214.  
  215. fetch(base_url + '/save-newsitem', { 
  216.  
  217. method: 'POST'
  218.  
  219. headers: { 
  220.  
  221. 'Accept''application/json'
  222.  
  223. 'Content-Type''application/json'
  224.  
  225. }, 
  226.  
  227. body: JSON.stringify({ 
  228.  
  229. news_title: this.state.news_title, 
  230.  
  231. news_url: this.state.news_url, 
  232.  
  233. }) 
  234.  
  235. }) 
  236.  
  237. .then((response) => { 
  238.  
  239. alert('News was shared!'); 
  240.  
  241. this.setState({ 
  242.  
  243. news_title: ''
  244.  
  245. news_url: '' 
  246.  
  247. }); 
  248.  
  249. }) 
  250.  
  251. .catch((err) => { 
  252.  
  253. alert('Error occured while sharing news'); 
  254.  
  255. }); 
  256.  
  257.  
  258. openPage(url){ 
  259.  
  260. Linking.canOpenURL(url).then(supported => { 
  261.  
  262. if(supported){ 
  263.  
  264. Linking.openURL(url); 
  265.  
  266.  
  267. }); 
  268.  
  269.  
  270. renderNews(news){ 
  271.  
  272. return ( 
  273.  
  274. <View style={styles.news_item}> 
  275.  
  276. <TouchableHighlight onPress={this.upvoteNewsItem.bind(this, news.id, news.upvotes)} underlayColor={"#E8E8E8"}> 
  277.  
  278. <View style={styles.upvote}> 
  279.  
  280. <Icon name="triangle-up" size={30} color="#666" /> 
  281.  
  282. <Text style={styles.upvote_text}>{news.upvotes}</Text> 
  283.  
  284. </View
  285.  
  286. </TouchableHighlight> 
  287.  
  288. <TouchableHighlight onPress={this.openPage.bind(this, news.url)} underlayColor={"#E8E8E8"}> 
  289.  
  290. <View style={styles.news_title}> 
  291.  
  292. <Text style={styles.news_item_text}>{news.title}</Text> 
  293.  
  294. </View
  295.  
  296. </TouchableHighlight> 
  297.  
  298. </View
  299.  
  300. ); 
  301.  
  302.  
  303. render(){ 
  304.  
  305. return ( 
  306.  
  307. <View style={styles.container}> 
  308.  
  309. <View style={styles.header}> 
  310.  
  311. <View style={styles.app_title}> 
  312.  
  313. <Text style={styles.header_text}>News Sharer</Text> 
  314.  
  315. </View
  316.  
  317. <View style={styles.header_button_container}> 
  318.  
  319. <Button onPress={this.openModal.bind(this)} style={styles.btn}> 
  320.  
  321. Share News 
  322.  
  323. </Button> 
  324.  
  325. </View
  326.  
  327. </View
  328.  
  329.  
  330. this.state.is_news_loaded && 
  331.  
  332. <View style={styles.body}> 
  333.  
  334. <ListView initialListSize={1} dataSource={this.state.news} style={styles.news} renderRow={this.renderNews.bind(this)}></ListView> 
  335.  
  336. </View
  337.  
  338.  
  339. <Modal 
  340.  
  341. isOpen={this.state.is_modal_open} 
  342.  
  343. style={styles.modal} 
  344.  
  345. position={"center"
  346.  
  347.  
  348. <View style={styles.modal_body}> 
  349.  
  350. <View style={styles.modal_header}> 
  351.  
  352. <Text style={styles.modal_header_text}>Share News</Text> 
  353.  
  354. </View
  355.  
  356. <View style={styles.input_row}> 
  357.  
  358. <TextInput 
  359.  
  360. style={{height: 40, borderColor: 'gray', borderWidth: 1}} 
  361.  
  362. onChangeText={(text) => this.setState({news_title: text})} 
  363.  
  364. value={this.state.news_title} 
  365.  
  366. placeholder="Title" 
  367.  
  368. /> 
  369.  
  370. </View
  371.  
  372. <View style={styles.input_row}> 
  373.  
  374. <TextInput 
  375.  
  376. style={{height: 40, borderColor: 'gray', borderWidth: 1}} 
  377.  
  378. onChangeText={(text) => this.setState({news_url: text})} 
  379.  
  380. value={this.state.news_url} 
  381.  
  382. placeholder="URL" 
  383.  
  384. keyboardType="url" 
  385.  
  386. /> 
  387.  
  388. </View
  389.  
  390. <View style={styles.input_row}> 
  391.  
  392. <Button onPress={this.shareNews.bind(this)} style={[styles.btn, styles.share_btn]}> 
  393.  
  394. Share 
  395.  
  396. </Button> 
  397.  
  398. </View
  399.  
  400. </View
  401.  
  402. </Modal> 
  403.  
  404. </View
  405.  
  406. ); 
  407.  
  408.  
  409.  
  410. const styles = StyleSheet.create({ 
  411.  
  412. container: { 
  413.  
  414. flex: 1, 
  415.  
  416. alignSelf: 'stretch'
  417.  
  418. backgroundColor: '#F5FCFF'
  419.  
  420. }, 
  421.  
  422. header: { 
  423.  
  424. flex: 1, 
  425.  
  426. backgroundColor: '#3B3738'
  427.  
  428. flexDirection: 'row' 
  429.  
  430. }, 
  431.  
  432. app_title: { 
  433.  
  434. flex: 7, 
  435.  
  436. padding: 10 
  437.  
  438. }, 
  439.  
  440. header_text: { 
  441.  
  442. fontSize: 20, 
  443.  
  444. color: '#FFF'
  445.  
  446. fontWeight: 'bold' 
  447.  
  448. }, 
  449.  
  450. header_button_container: { 
  451.  
  452. flex: 3 
  453.  
  454. }, 
  455.  
  456. body: { 
  457.  
  458. flex: 19 
  459.  
  460. }, 
  461.  
  462. btn: { 
  463.  
  464. backgroundColor: "#0***5D1"
  465.  
  466. color: "white"
  467.  
  468. margin: 10 
  469.  
  470. }, 
  471.  
  472. modal: { 
  473.  
  474. height: 300 
  475.  
  476. }, 
  477.  
  478. modal_header: { 
  479.  
  480. margin: 20, 
  481.  
  482. }, 
  483.  
  484. modal_body: { 
  485.  
  486. alignItems: 'center' 
  487.  
  488. }, 
  489.  
  490. input_row: { 
  491.  
  492. padding: 20 
  493.  
  494. }, 
  495.  
  496. modal_header_text: { 
  497.  
  498. fontSize: 18, 
  499.  
  500. fontWeight: 'bold' 
  501.  
  502. }, 
  503.  
  504. share_btn: { 
  505.  
  506. width: 100 
  507.  
  508. }, 
  509.  
  510. news_item: { 
  511.  
  512. paddingLeft: 10, 
  513.  
  514. paddingRight: 10, 
  515.  
  516. paddingTop: 15, 
  517.  
  518. paddingBottom: 15, 
  519.  
  520. marginBottom: 5, 
  521.  
  522. borderBottomWidth: 1, 
  523.  
  524. borderBottomColor: '#ccc'
  525.  
  526. flex: 1, 
  527.  
  528. flexDirection: 'row' 
  529.  
  530. }, 
  531.  
  532. news_item_text: { 
  533.  
  534. color: '#575757'
  535.  
  536. fontSize: 18 
  537.  
  538. }, 
  539.  
  540. upvote: { 
  541.  
  542. flex: 2, 
  543.  
  544. paddingRight: 15, 
  545.  
  546. paddingLeft: 5, 
  547.  
  548. alignItems: 'center' 
  549.  
  550. }, 
  551.  
  552. news_title: { 
  553.  
  554. flex: 18, 
  555.  
  556. justifyContent: 'center' 
  557.  
  558. }, 
  559.  
  560. upvote_text: { 
  561.  
  562. fontSize: 18, 
  563.  
  564. fontWeight: 'bold' 
  565.  
  566.  
  567. }); 
  568.  
  569. AppRegistry.registerComponent('Main', () => Main); 

下面来分析一下上面代码。首先,导入编程中所需要的内置的React Native及第三方组件:

  1. import React, { Component } from 'react'
  2.  
  3. import { 
  4.  
  5. AppRegistry, 
  6.  
  7. StyleSheet, 
  8.  
  9. Text, 
  10.  
  11. View
  12.  
  13. TextInput, 
  14.  
  15. TouchableHighlight, 
  16.  
  17. Linking, 
  18.  
  19. ListView 
  20.  
  21. from 'react-native'
  22.  
  23. import Button from 'react-native-button'
  24.  
  25. import Modal from 'react-native-modalbox'
  26.  
  27. import Icon from 'react-native-vector-icons/Octicons'
  28.  
  29. import "../UserAgent"
  30.  
  31. import io from 'socket.io-client/socket.io'
  32.  
  33. import _ from 'lodash'

注意,你使用如下方式导入了自己开发的另外文件中的代码:

  1. import "../UserAgent"

这是你在根目录NewsSharer下看到的UserAgent.js文件。它包含的代码用于设置用户代理为react-native——Socket.io需要这样做,或者它会假设程序运行于浏览器环境中。

  1. window.navigator.userAgent = 'react-native'

接下来,确定应用程序要请求的基URL。如果您要进行本地测试,这可能是您的计算机的内部IP地址。为了使这能够工作,你必须确保你的手机或平板电脑连接到与您的计算机位于同一网络。

  1. var base_url = 'http://YOUR_DOMAIN_NAME_OR_IP_ADDRESS:3000'

接下来,在构造函数中,初始化套接字连接:

  1. this.socket = io(base_url, { 
  2.  
  3. transports: ['websocket'
  4.  
  5. }); 

然后,设置应用程序的默认状态:

  1. this.state = { 
  2.  
  3. is_modal_open: false, //for showing/hiding the modal 
  4.  
  5. news_title: '', //default value for news title text field 
  6.  
  7. news_url: '', //default value for news url text field 
  8.  
  9. //initialize a datasource for the news items 
  10.  
  11. news_items_datasource: new ListView.DataSource({ 
  12.  
  13. rowHasChanged: (row1, row2) => row1 !== row2, 
  14.  
  15. }), 
  16.  
  17. //for showing/hiding the news items 
  18.  
  19. is_news_loaded: false
  20.  
  21. news: {}, //the news items datasource 
  22.  
  23. news_items: [] //the news items 
  24.  
  25. }; 

此函数的功能是使用内置的fetch方法从服务器端取回新闻项目。它向news路由发出GET请求,然后从响应中提取news_items对象。这个对象用于稍后创建客户端ListView组件所需的新闻数据源。一旦创建,它便使用新闻数据源更新状态;这样一来,用户界面新闻项内容也可以得到相应的更新。

  1. getNewsItems(){ 
  2.  
  3. fetch(base_url + '/news'
  4.  
  5. .then((response) => { 
  6.  
  7. return response.json(); 
  8.  
  9. }) 
  10.  
  11. .then((news_items) => { 
  12.  
  13. this.setState({ 
  14.  
  15. 'news_items': news_items 
  16.  
  17. }); 
  18.  
  19. var news_datasource = this.state.news_items_datasource.cloneWithRows(news_items); 
  20.  
  21. this.setState({ 
  22.  
  23. 'news': news_datasource, 
  24.  
  25. 'is_news_loaded'true 
  26.  
  27. }); 
  28.  
  29. return news_items; 
  30.  
  31. }) 
  32.  
  33. .catch((error) => { 
  34.  
  35. alert('Error occured while fetching news items'); 
  36.  
  37. }); 
  38.  

下面的ComponentWillMount方法是React的生命周期方法之一。这允许您可以在初始化渲染发生前执行你自己的定制代码。也正是在此处,你监听Socket.io的服务器组件发出的news_updated事件;而当此事件发生时,它可能是两件事中之一——或者当用户共享新闻项时或者当他们对现有新闻项投赞成票时。

值得注意的是,当出现新的新闻项时RethinkDB的changefeed将为old_val返回一个null值。这也正是我们区分上面两种可能性的办法。如果用户共享一个新闻项,那么将其推到news_items数组中;否则,查找投赞成票的新闻项并更新其赞成票计数。现在,您可以更新用户界面来反映所做的更改了。

  1. componentWillMount(){ 
  2.  
  3. this.socket.on('news_updated', (data) => { 
  4.  
  5. var news_items = this.state.news_items; 
  6.  
  7. if(data.old_val === null){ //a new news item is shared 
  8.  
  9. //push the new item to the news_items array 
  10.  
  11. news_items.push(data.new_val); 
  12.  
  13. }else{ //an existing news item is upvoted 
  14.  
  15. //find the news item that was upvoted and update its upvote count 
  16.  
  17. _.map(news_items, function(row, index){ 
  18.  
  19. if(row.id == data.new_val.id){ 
  20.  
  21. news_items[index].upvotes = data.new_val.upvotes; 
  22.  
  23.  
  24. }); 
  25.  
  26.  
  27. //update the UI to reflect the changes 
  28.  
  29. this.updateUI(news_items); 
  30.  
  31. }); 
  32.  

接下来,UpdateUI函数使用赞成票数按照从高到低订阅新闻项。一旦排序,便提取最前面的30条新闻,同时更新状态。

  1. updateUI(news_items){ 
  2.  
  3. var ordered_news_items = _.orderBy(news_items, 'upvotes''desc'); 
  4.  
  5. var limited_news_items = _.slice(ordered_news_items, 0, 30); 
  6.  
  7. var news_datasource = this.state.news_items_datasource.cloneWithRows(limited_news_items); 
  8.  
  9. this.setState({ 
  10.  
  11. 'news': news_datasource, 
  12.  
  13. 'is_news_loaded'true
  14.  
  15. 'is_modal_open'false
  16.  
  17. 'news_items': limited_news_items 
  18.  
  19. }); 
  20.  

下面要介绍的ComponentDidMount方法是另一个React生命周期方法,此方法在初始渲染之后调用。在此方法中,我们实现从服务器端获取新闻项。

【注】,如果你想在安装组件之前发出请求的话,也可以从componentWillMount方法中从服务器端获取新闻项。

  1. componentDidMount(){ 
  2.  
  3. this.getNewsItems(); 
  4.  

接下来的upvoteNewsItem方法将向服务器端发出一个投赞成票新闻项请求:

  1. upvoteNewsItem(id, upvotes){ 
  2.  
  3. fetch(base_url + '/upvote-newsitem', { 
  4.  
  5. method: 'POST'
  6.  
  7. headers: { 
  8.  
  9. 'Accept''application/json'
  10.  
  11. 'Content-Type''application/json'
  12.  
  13. }, 
  14.  
  15. body: JSON.stringify({ 
  16.  
  17. news_id: id, 
  18.  
  19. upvotes: upvotes + 1 
  20.  
  21. }) 
  22.  
  23. }) 
  24.  
  25. .catch((err) => { 
  26.  
  27. alert('Error occured while trying to upvote'); 
  28.  
  29. }); 
  30.  

接下来,openModal和closeModal方法分别负责显示与隐藏共享新闻内容的模态对话框。

  1. openModal(){ 
  2.  
  3. this.setState({ 
  4.  
  5. is_modal_open: true 
  6.  
  7. }); 
  8.  
  9.  
  10. closeModal(){ 
  11.  
  12. this.setState({ 
  13.  
  14. is_modal_open: false 
  15.  
  16. }); 
  17.  

继续往下来,shareNews函数用于发送请求来创建一条新闻项:

  1. shareNews(){ 
  2.  
  3. fetch(base_url + '/save-newsitem', { 
  4.  
  5. method: 'POST'
  6.  
  7. headers: { 
  8.  
  9. 'Accept''application/json'
  10.  
  11. 'Content-Type''application/json'
  12.  
  13. }, 
  14.  
  15. body: JSON.stringify({ 
  16.  
  17. news_title: this.state.news_title, 
  18.  
  19. news_url: this.state.news_url, 
  20.  
  21. }) 
  22.  
  23. }) 
  24.  
  25. .then((response) => { 
  26.  
  27. alert('News was shared!'); 
  28.  
  29. this.setState({ 
  30.  
  31. news_title: ''
  32.  
  33. news_url: '' 
  34.  
  35. }); 
  36.  
  37. }) 
  38.  
  39. .catch((err) => { 
  40.  
  41. alert('Error occured while sharing news'); 
  42.  
  43. }); 
  44.  

再往下,openPage函数用于在浏览器中打开新闻项对应的URL:

  1. openPage(url){ 
  2.  
  3. Linking.canOpenURL(url).then(supported => { 
  4.  
  5. if(supported){ 
  6.  
  7. Linking.openURL(url); 
  8.  
  9.  
  10. }); 
  11.  

接下来,RenderNews函数将针对每个新闻项返回UI。这个方法中还负责显示“upvote”按钮、赞成票数和新闻标题。其中,新闻标题被封装在一个TouchableHighlight组件。这允许我们通过执行openPage函数来打开对应的URL。对于赞成票数,也要这样做。

【注意】该代码使用了TouchableHighlight组件而不是Button组件,因为Button组件不能内含View或Text组件。

  1. renderNews(news){ 
  2.  
  3. return ( 
  4.  
  5. <View style={styles.news_item}> 
  6.  
  7. <TouchableHighlight onPress={this.upvoteNewsItem.bind(this, news.id, news.upvotes)} underlayColor={"#E8E8E8"}> 
  8.  
  9. <View style={styles.upvote}> 
  10.  
  11. <Icon name="triangle-up" size={30} color="#666" /> 
  12.  
  13. <Text style={styles.upvote_text}>{news.upvotes}</Text> 
  14.  
  15. </View
  16.  
  17. </TouchableHighlight> 
  18.  
  19. <TouchableHighlight onPress={this.openPage.bind(this, news.url)} underlayColor={"#E8E8E8"}> 
  20.  
  21. <View style={styles.news_title}> 
  22.  
  23. <Text style={styles.news_item_text}>{news.title}</Text> 
  24.  
  25. </View
  26.  
  27. </TouchableHighlight> 
  28.  
  29. </View
  30.  
  31. ); 
  32.  

再往下,render函数负责返回整个应用程序的UI部分:

  1. render(){ 
  2.  
  3. ... 
  4.  

在render函数中,要建立包含应用程序的标题的标题和一个按钮用于打开模态对话框来分享新闻项的按钮。

  1. <View style={styles.header}> 
  2.  
  3. <View style={styles.app_title}> 
  4.  
  5. <Text style={styles.header_text}>News Sharer</Text> 
  6.  
  7. </View
  8.  
  9. <View style={styles.header_button_container}> 
  10.  
  11. <Button onPress={this.openModal.bind(this)} style={styles.btn}> 
  12.  
  13. Share News 
  14.  
  15. </Button> 
  16.  
  17. </View
  18.  
  19. </View

对于body部分,使用ListView组件来渲染新闻项。它有三个必需的参数:initialListSize,dataSource和renderRow。其中,InitialListSize被设置为1;这样一来,ListView就能够针对内容部分的每一个帧逐行渲染。如果你想一次显示所有行的话,你还可以把这值修改得更大些。dataSource对应于新闻项,renderRow函数用于渲染每一行中的新闻项。

  1.  
  2. this.state.is_news_loaded && 
  3.  
  4. <View style={styles.body}> 
  5.  
  6. <ListView initialListSize={1} dataSource={this.state.news} style={styles.news} renderRow={this.renderNews.bind(this)}></ListView> 
  7.  
  8. </View
  9.  

接下来是定义分享新闻的模态对话框。此对话框中使用了两个文本字段分别用于输入标题和新闻URL,还有一个按钮用于将新闻提交到服务器。文本字段使用了TextInput组件实现。由于没有使用标签控件,所以需要在TextInput组件中输入占位符文本来提示用户要输入的内容。

这两个文本字段都有一个onChangeText方法,在文本值更新时使用。keyboardType的Url用于新闻URL的文本字段;这样的话,它将打开设备的键盘,实现输入URL的优化支持。用户不必手动输入内容,可以使用拷贝和粘贴。文本字段的下方是用于共享新闻的按钮。按钮的点击将调用先前定义的shareNews函数。

  1. <Modal 
  2.  
  3. isOpen={this.state.is_modal_open} 
  4.  
  5. style={styles.modal} 
  6.  
  7. position={"center"
  8.  
  9.  
  10. <View style={styles.modal_body}> 
  11.  
  12. <View style={styles.modal_header}> 
  13.  
  14. <Text style={styles.modal_header_text}>Share News</Text> 
  15.  
  16. </View
  17.  
  18. <View style={styles.input_row}> 
  19.  
  20. <TextInput 
  21.  
  22. style={{height: 40, borderColor: 'gray', borderWidth: 1}} 
  23.  
  24. onChangeText={(text) => this.setState({news_title: text})} 
  25.  
  26. value={this.state.news_title} 
  27.  
  28. placeholder="Title" 
  29.  
  30. /> 
  31.  
  32. </View
  33.  
  34. <View style={styles.input_row}> 
  35.  
  36. <TextInput 
  37.  
  38. style={{height: 40, borderColor: 'gray', borderWidth: 1}} 
  39.  
  40. onChangeText={(text) => this.setState({news_url: text})} 
  41.  
  42. value={this.state.news_url} 
  43.  
  44. placeholder="URL" 
  45.  
  46. keyboardType="url" 
  47.  
  48. /> 
  49.  
  50. </View
  51.  
  52. <View style={styles.input_row}> 
  53.  
  54. <Button onPress={this.shareNews.bind(this)} style={[styles.btn, styles.share_btn]}> 
  55.  
  56. Share 
  57.  
  58. </Button> 
  59.  
  60. </View
  61.  
  62. </View
  63.  
  64. </Modal> 

接下来,为组件设置样式:

  1. const styles = StyleSheet.create({ 
  2.  
  3. container: { 
  4.  
  5. flex: 1, 
  6.  
  7. alignSelf: 'stretch'
  8.  
  9. backgroundColor: '#F5FCFF'
  10.  
  11. }, 
  12.  
  13. header: { 
  14.  
  15. flex: 1, 
  16.  
  17. backgroundColor: '#3B3738'
  18.  
  19. flexDirection: 'row' 
  20.  
  21. }, 
  22.  
  23. app_title: { 
  24.  
  25. flex: 7, 
  26.  
  27. padding: 10 
  28.  
  29. }, 
  30.  
  31. header_text: { 
  32.  
  33. fontSize: 20, 
  34.  
  35. color: '#FFF'
  36.  
  37. fontWeight: 'bold' 
  38.  
  39. }, 
  40.  
  41. header_button_container: { 
  42.  
  43. flex: 3 
  44.  
  45. }, 
  46.  
  47. body: { 
  48.  
  49. flex: 19 
  50.  
  51. }, 
  52.  
  53. btn: { 
  54.  
  55. backgroundColor: "#0***5D1"
  56.  
  57. color: "white"
  58.  
  59. margin: 10 
  60.  
  61. }, 
  62.  
  63. modal: { 
  64.  
  65. height: 300 
  66.  
  67. }, 
  68.  
  69. modal_header: { 
  70.  
  71. margin: 20, 
  72.  
  73. }, 
  74.  
  75. modal_body: { 
  76.  
  77. alignItems: 'center' 
  78.  
  79. }, 
  80.  
  81. input_row: { 
  82.  
  83. padding: 20 
  84.  
  85. }, 
  86.  
  87. modal_header_text: { 
  88.  
  89. fontSize: 18, 
  90.  
  91. fontWeight: 'bold' 
  92.  
  93. }, 
  94.  
  95. share_btn: { 
  96.  
  97. width: 100 
  98.  
  99. }, 
  100.  
  101. news_item: { 
  102.  
  103. paddingLeft: 10, 
  104.  
  105. paddingRight: 10, 
  106.  
  107. paddingTop: 15, 
  108.  
  109. paddingBottom: 15, 
  110.  
  111. marginBottom: 5, 
  112.  
  113. borderBottomWidth: 1, 
  114.  
  115. borderBottomColor: '#ccc'
  116.  
  117. flex: 1, 
  118.  
  119. flexDirection: 'row' 
  120.  
  121. }, 
  122.  
  123. news_item_text: { 
  124.  
  125. color: '#575757'
  126.  
  127. fontSize: 18 
  128.  
  129. }, 
  130.  
  131. upvote: { 
  132.  
  133. flex: 2, 
  134.  
  135. paddingRight: 15, 
  136.  
  137. paddingLeft: 5, 
  138.  
  139. alignItems: 'center' 
  140.  
  141. }, 
  142.  
  143. news_title: { 
  144.  
  145. flex: 18, 
  146.  
  147. justifyContent: 'center' 
  148.  
  149. }, 
  150.  
  151. upvote_text: { 
  152.  
  153. fontSize: 18, 
  154.  
  155. fontWeight: 'bold' 
  156.  
  157.  
  158. }); 

开发服务器端组件

现在正是时候要移动到的服务器组件的应用程序,在这里,您将学习如何保存和 RethinkDB,upvote 新闻项目以及如何通知应用程序在数据库中发生了变化。

创建数据库

我假定您已经在您的计算机上安装了RethinkDB。否则的话,请按照RethinkDB网站上的提示(https://www.rethinkdb.com/docs/install/)先行安装吧。

安装完毕后,您现在可以打开浏览器访问http://localhost:8080来查看RethinkDB管理控制台。在Tables选项卡上单击,然后单击Add Database按钮。这将打开一个模态窗口,允许您输入数据库的名称,称之为newssharer吧,***单击Click。

现在来创建要在其中保存新闻条目的表。单击Add Table按钮,命名为news_items,然后单击Create Table。

安装依赖性

您可以导航到项目的根目录 (即newssharer-server.js和package.json文件所在位置),执行npm install命令来安装以下服务器依赖项:

1.Express: 基于Node.js的web框架,允许您创建响应特定路由的web服务器。

2.Body-parser:便于从请求正文中提取JSON字符串。

3.Rethinkdb:Node.js的RethinkDB客户端。

4.socket.io:一个实时框架,当有人分享新闻或对现有新闻投赞成票时允许您连接到所有的客户端。

服务端编程

文件newssharer-server.js的代码如下:

  1. var r = require('rethinkdb'); 
  2.  
  3. var express = require('express'); 
  4.  
  5. var app = express(); 
  6.  
  7. var server = require('http').createServer(app); 
  8.  
  9. var io = require('socket.io')(server); 
  10.  
  11. var bodyParser = require('body-parser'); 
  12.  
  13. app.use(bodyParser.json()); 
  14.  
  15. var connection
  16.  
  17. r.connect({host: 'localhost', port: 28015}, function(err, conn) { 
  18.  
  19. if(err) throw err; 
  20.  
  21. connection = conn; 
  22.  
  23. r.db('newssharer').table('news_items'
  24.  
  25. .orderBy({index: r.desc('upvotes')}) 
  26.  
  27. .changes() 
  28.  
  29. .run(connectionfunction(err, cursor){ 
  30.  
  31. if (err) throw err; 
  32.  
  33. io.sockets.on('connection'function(socket){ 
  34.  
  35. cursor.each(function(err, row){ 
  36.  
  37. if(err) throw err; 
  38.  
  39. io.sockets.emit('news_updated', row); 
  40.  
  41. }); 
  42.  
  43. }); 
  44.  
  45. }); 
  46.  
  47. }); 
  48.  
  49. app.get('/create-table'function(req, res){ 
  50.  
  51. r.db('newssharer').table('news_items').indexCreate('upvotes').run(connectionfunction(err, result){ 
  52.  
  53. console.log('boom'); 
  54.  
  55. res.send('ok'
  56.  
  57. }); 
  58.  
  59. }); 
  60.  
  61. app.get('/fill'function(req, res){ 
  62.  
  63. r.db('newssharer').table('news_items').insert([ 
  64.  
  65.  
  66. title: 'A Conversation About Fantasy User Interfaces'
  67.  
  68. url: 'https://www.subtraction.com/2016/06/02/a-conversation-about-fantasy-user-interfaces/'
  69.  
  70. upvotes: 30 
  71.  
  72. }, 
  73.  
  74.  
  75. title: 'Apple Cloud Services Outage'
  76.  
  77. url: 'https://www.apple.com/support/systemstatus/'
  78.  
  79. upvotes: 20 
  80.  
  81.  
  82. ]).run(connectionfunction(err, result){ 
  83.  
  84. if (err) throw err; 
  85.  
  86. res.send('news_items table was filled!'); 
  87.  
  88. }); 
  89.  
  90. }); 
  91.  
  92. app.get('/news'function(req, res){ 
  93.  
  94. res.header("Content-Type""application/json"); 
  95.  
  96. r.db('newssharer').table('news_items'
  97.  
  98. .orderBy({index: r.desc('upvotes')}) 
  99.  
  100. .limit(30) 
  101.  
  102. .run(connectionfunction(err, cursor) { 
  103.  
  104. if (err) throw err; 
  105.  
  106. cursor.toArray(function(err, result) { 
  107.  
  108. if (err) throw err; 
  109.  
  110. res.send(result); 
  111.  
  112. }); 
  113.  
  114. }); 
  115.  
  116. }); 
  117.  
  118. app.post('/save-newsitem'function(req, res){ 
  119.  
  120. var news_title = req.body.news_title; 
  121.  
  122. var news_url = req.body.news_url; 
  123.  
  124. r.db('newssharer').table('news_items').insert([ 
  125.  
  126.  
  127. 'title': news_title, 
  128.  
  129. 'url': news_url, 
  130.  
  131. 'upvotes': 100 
  132.  
  133. }, 
  134.  
  135. ]).run(connectionfunction(err, result){ 
  136.  
  137. if (err) throw err; 
  138.  
  139. res.send('ok'); 
  140.  
  141. }); 
  142.  
  143. }); 
  144.  
  145. app.post('/upvote-newsitem'function(req, res){ 
  146.  
  147. var id = req.body.news_id; 
  148.  
  149. var upvote_count = req.body.upvotes; 
  150.  
  151. r.db('newssharer').table('news_items'
  152.  
  153. .filter(r.row('id').eq(id)) 
  154.  
  155. .update({upvotes: upvote_count}) 
  156.  
  157. .run(connectionfunction(err, result) { 
  158.  
  159. if (err) throw err; 
  160.  
  161. res.send('ok'); 
  162.  
  163. }); 
  164.  
  165. }); 
  166.  
  167. app.get('/test/upvote'function(req, res){ 
  168.  
  169. var id = '144f7d7d-d580-42b3-8704-8372e9b2a17c'
  170.  
  171. var upvote_count = 350; 
  172.  
  173. r.db('newssharer').table('news_items'
  174.  
  175. .filter(r.row('id').eq(id)) 
  176.  
  177. .update({upvotes: upvote_count}) 
  178.  
  179. .run(connectionfunction(err, result) { 
  180.  
  181. if (err) throw err; 
  182.  
  183. res.send('ok'); 
  184.  
  185. }); 
  186.  
  187. }); 
  188.  
  189. app.get('/test/save-newsitem'function(req, res){ 
  190.  
  191. r.db('newssharer').table('news_items').insert([ 
  192.  
  193.  
  194. 'title''banana'
  195.  
  196. 'url''http://banana.com'
  197.  
  198. 'upvotes': 190, 
  199.  
  200. 'downvotes': 0 
  201.  
  202. }, 
  203.  
  204. ]).run(connectionfunction(err, result){ 
  205.  
  206. if(err) throw err; 
  207.  
  208. res.send('ok'); 
  209.  
  210. }); 
  211.  
  212. }); 
  213.  
  214. server.listen(3000); 

在上面的代码中,您首先导入依赖项:

  1. var r = require('rethinkdb'); 
  2.  
  3. var express = require('express'); 
  4.  
  5. var app = express(); 
  6.  
  7. var server = require('http').createServer(app); 
  8.  
  9. var io = require('socket.io')(server); 
  10.  
  11. var bodyParser = require('body-parser'); 
  12.  
  13. app.use(bodyParser.json()); 

然后,创建用于存储当前的RethinkDB连接的变量。

  1. var connection

监听变化

连接到RethinkDB数据库,默认情况下在端口28015(即创建连接的地址处)上运行RethinkDB。如果你想使用不同的端口,可以将28015替换为你所使用的端口。

  1. r.connect({host: 'localhost', port: 28015}, function(err, conn) { 
  2.  
  3. if(err) throw err; 
  4.  
  5. connection = conn; 
  6.  
  7. ... 
  8.  
  9. }); 

还是在数据库连接代码中,查询newssharer数据库中的表news_items,并按投票计数排序项目。然后,使用RethinkDB的Changefeeds功能来侦听表(数据库排序日志)中的更改。表中发生每一次变化(CRUD操作),都会发出此通知。

  1. r.db('newssharer').table('news_items'
  2.  
  3. .orderBy({index: r.desc('upvotes')}) 
  4.  
  5. .changes() 
  6.  
  7. .run(connectionfunction(err, cursor){ 
  8.  
  9. ... 
  10.  
  11. }); 

在run方法里面的回调函数中,初始化套接字连接并循环遍历cursor的内容。Cursor描述了表中所做的更改。每次发生更改时,都会触发cursor.each函数。

【注意】该函数并不包含所有的数据更改。每当有新的更改时,获取替换以前的更改。这意味着,在任何给定时间内只能遍历单行。这将允许您使用socket.io来把更改发送到客户端。

  1. if (err) throw err; //check if there are errors and return it if any 
  2.  
  3. io.sockets.on('connection'function(socket){ 
  4.  
  5. cursor.each(function(err, row){ 
  6.  
  7. if(err) throw err; 
  8.  
  9. io.sockets.emit('news_updated', row); 
  10.  
  11. }); 
  12.  
  13. }); 

如果有新闻项被共享,那么每一行都有下列结构:

  1.  
  2. "old_val"null
  3.  
  4. "new_val": { 
  5.  
  6. "id": 1, 
  7.  
  8. "news_title""Google"
  9.  
  10. "news_url""http://google.com"
  11.  
  12. "upvotes": 0 
  13.  
  14.  

这就是为什么前面代码中检查null,因为一个新建的新闻项不会有一个old_val。

如果用户对一个新闻项投赞成票:

  1.  
  2. "old_val": { 
  3.  
  4. "id": 1, 
  5.  
  6. "news_title""Google"
  7.  
  8. "news_url""http://google.com"
  9.  
  10. "upvotes": 0 
  11.  
  12.  
  13. "new_val": { 
  14.  
  15. "id": 1, 
  16.  
  17. "news_title""Google"
  18.  
  19. "news_url""http://google.com"
  20.  
  21. "upvotes": 1 
  22.  
  23.  

那么,将返回该行中的对应于旧值和新值的完整结构。这意味着,你可以在一个客户端更新多个字段,并且可以把所有这些变化发送给其他相连接的客户端。借助于RethinkDB的changfeeds特性,基于RethinkDB开发实时应用变得特别简单。

对Upvotes字段添加索引

下面这个路由把一个索引添加到upvotes字段上:

  1. app.get('/add-index'function(req, res){ 
  2.  
  3. r.db('newssharer').table('news_items').indexCreate('upvotes').run(connectionfunction(err, result){ 
  4.  
  5. res.send('ok'
  6.  
  7. }); 
  8.  
  9. }); 

上述创建索引操作对于orderBy功能来说是必需的,因为它需要你排序的字段有一个索引。

  1. .orderBy({index: r.desc('upvotes')}) 

当服务器运行时,在测试应用前请确保在你的浏览器中打开网址http://localhost:3000/add-index。注意,上面这个路由仅需要调用一次。

添加空新闻项

下面这个路由将在news_items表中插入一个空的入口。实际上这是用于测试目的的一个可选功能;这样一来,在不需要使用程序添加的情况下你会立即看到表中出现一个新项。

  1. app.get('/fill'function(req, res){ 
  2.  
  3. r.db('newssharer').table('news_items').insert([ 
  4.  
  5.  
  6. title: 'A Conversation About Fantasy User Interfaces'
  7.  
  8. url: 'https://www.subtraction.com/2016/06/02/a-conversation-about-fantasy-user-interfaces/'
  9.  
  10. upvotes: 30 
  11.  
  12. }, 
  13.  
  14.  
  15. title: 'Apple Cloud Services Outage'
  16.  
  17. url: 'https://www.apple.com/support/systemstatus/'
  18.  
  19. upvotes: 20 
  20.  
  21.  
  22. ]).run(connectionfunction(err, result){ 
  23.  
  24. if (err) throw err; 
  25.  
  26. res.send('news_items table was filled!'); 
  27.  
  28. }); 
  29.  
  30. }); 

返回新闻项

下面的路由将返回新闻项:

  1. app.get('/news'function(req, res){ 
  2.  
  3. res.header("Content-Type""application/json"); 
  4.  
  5. r.db('newssharer').table('news_items'
  6.  
  7. .orderBy({index: r.desc('upvotes')}) 
  8.  
  9. .limit(30) 
  10.  
  11. .run(connectionfunction(err, cursor) { 
  12.  
  13. if (err) throw err; 
  14.  
  15. cursor.toArray(function(err, result) { 
  16.  
  17. if (err) throw err; 
  18.  
  19. res.send(result); 
  20.  
  21. }); 
  22.  
  23. }); 
  24.  
  25. }); 

注意到,该新闻项按照赞成票数从高到低的顺序排序,并且限定为最多30条。另外,这里没有使用cursor.each来遍历新闻项,而是使用cursor.toArray并通过如下结构把新闻项转换成一个数组:

  1.  
  2.  
  3. "title""A Conversation About Fantasy User Interfaces"
  4.  
  5. "url""https://www.subtraction.com/2016/06/02/a-conversation-about-fantasy-user-interfaces/"
  6.  
  7. "upvotes": 30 
  8.  
  9. }, 
  10.  
  11.  
  12. "title""Apple Cloud Services Outage"
  13.  
  14. "url""https://www.apple.com/support/systemstatus/"
  15.  
  16. "upvotes": 20 
  17.  
  18.  

新建与保存新闻项

下面的路由实现新建与保存新闻项功能:

  1. app.post('/save-newsitem'function(req, res){ 
  2.  
  3. var news_title = req.body.news_title; 
  4.  
  5. var news_url = req.body.news_url; 
  6.  
  7. r.db('newssharer').table('news_items').insert([ 
  8.  
  9.  
  10. 'title': news_title, 
  11.  
  12. 'url': news_url, 
  13.  
  14. 'upvotes': 100 
  15.  
  16. }, 
  17.  
  18. ]).run(connectionfunction(err, result){ 
  19.  
  20. if (err) throw err; 
  21.  
  22. res.send('ok'); 
  23.  
  24. }); 
  25.  
  26. }); 

当一个用户共享应用程序中的新闻项时将调用上面的路由。它接收来自于请求正文(Request Body)的新闻标题和URL数据。最初的赞成票数设置为100,但您可以选择另一个数字。

对新闻项投赞成票

下面的路由实现对新闻项投赞成票功能:

  1. app.post('/upvote-newsitem'function(req, res){ 
  2.  
  3. var id = req.body.news_id; 
  4.  
  5. var upvote_count = req.body.upvotes; 
  6.  
  7. r.db('newssharer').table('news_items'
  8.  
  9. .filter(r.row('id').eq(id)) 
  10.  
  11. .update({upvotes: upvote_count}) 
  12.  
  13. .run(connectionfunction(err, result) { 
  14.  
  15. if (err) throw err; 
  16.  
  17. res.send('ok'); 
  18.  
  19. }); 
  20.  
  21. }); 

当用户在程序中对新闻项投赞成票时将调用上面的路由函数。该函数使用新闻项的ID来取回新闻数据并更新之。

【注意】你已经在应用程序中把upvotes值加1了;因此,也就已经提供了请求主体(Request Body)内的数据。

测试保存与投赞成票功能

至此,我们已经建立了好几个路由。现在,不妨来测试一下保存功能与投赞成票功能。实现这一测试的***时机是当应用程序已经运行于移动设备上时。如此一来,你便能够看到UI更新情况。我们将在下一节讨论如何运行程序的问题。

下面的路由实现测试保存功能:

  1. app.get('/test/save-newsitem'function(req, res){ 
  2.  
  3. r.db('newssharer').table('news_items').insert([ 
  4.  
  5.  
  6. 'title''banana'
  7.  
  8. 'url''http://banana.com'
  9.  
  10. 'upvotes': 190, 
  11.  
  12. 'downvotes': 0 
  13.  
  14. }, 
  15.  
  16. ]).run(connectionfunction(err, result){ 
  17.  
  18. if(err) throw err; 
  19.  
  20. res.send('ok'); 
  21.  
  22. }); 
  23.  
  24. }); 

下面的路由实现对新闻项投赞成票的测试功能。记住一定要使用已有新闻项的ID来取代程序中的ID才能使测试正常进行:

  1. app.get('/test/upvote'function(req, res){ 
  2.  
  3. var id = '144f7d7d-d580-42b3-8704-8372e9b2a17c'
  4.  
  5. var upvote_count = 350; 
  6.  
  7. r.db('newssharer').table('news_items'
  8.  
  9. .filter(r.row('id').eq(id)) 
  10.  
  11. .update({upvotes: upvote_count}) 
  12.  
  13. .run(connectionfunction(err, result) { 
  14.  
  15. if (err) throw err; 
  16.  
  17. res.send('ok'); 
  18.  
  19. }); 
  20.  
  21. }); 

运行服务器端程序

到此,我假定RethinkDB一直运行于后台中。如果还没有运行起来,请先运行它。一旦RethinkDB运行后,你就可以在项目根目录下执行命令node newssharer-server.js来运行程序的服务器端组件了。

运行移动客户端程序

你可以使用与任何React Native程序一样的方式来启动你自己的应用程序。下面提供了在不同平台运行程序的链接,供参考:

1. 运行于Android平台的参考地址是:https://facebook.github.io/react-native/docs/running-on-device-android.html#content

2. 运行于iOS平台的参考地址是:https://facebook.github.io/react-native/docs/running-on-device-ios.html#content

一旦应用程序正常运行,请试着在浏览器中访问所有的测试例程。

小结

本文中提供的示例仅仅实际了最基本的部分,你还可以通过下面几个方面进一步改进这个程序:

1.不是通过移动设备的浏览器打开新闻内容,而是使用React Native的WebView组件在应用程序内创建一个webview的方式进行。

2.当前的程序允许用户不停地点击“upvote”按钮,你不妨添加一个功能来检查是否当前用户已经对当前新闻项投了赞成票。

3.修改服务器端程序,使之仅接收来自我们自己的应用程序发来的请求。

总而言之,通过本文的学习,你应当能够使用Socket.io和RethinkDB的changefeeds功能来创建一个基本型实时的新闻分享程序。

 

责任编辑:赵立京 来源: 51CTO
相关推荐

2013-11-19 15:35:01

2011-05-06 15:31:28

moblweb开发DSL

2010-10-15 09:39:22

MeeGoQt

2011-12-06 10:10:59

云计算移动应用

2011-02-22 10:23:43

2011-11-29 16:07:36

移动Web开发框架移动开发

2013-08-08 09:48:10

Web

2012-03-21 09:36:33

ibmdw

2012-05-14 17:35:28

移动Web

2010-08-18 09:23:19

Flash Lite移动应用程序开发

2014-03-27 10:28:31

移动Web开发框架

2015-03-20 10:31:10

移动Web

2010-07-28 19:24:10

2020-10-14 15:05:02

React应用程序

2013-02-22 09:28:45

MEAP软件移动应用开发HTML5

2010-08-11 09:45:03

2018-12-28 14:10:57

开发工具 移动应用

2009-09-22 12:22:54

ibmdwLotus

2016-06-06 17:26:22

平台开发

2019-08-29 09:00:55

开发Flutter框架
点赞
收藏

51CTO技术栈公众号