React 与 React-Native 使用同一个 meteor 后台
meteor 可以快速构建 pc,移动端,桌面端应用。
最大的优点是:数据库的数据发生变化时,可以实时推送到前端,非常适用于实时展示的应用开发。
在 react,react-native 应用中,可以仅使用同一个 meteor 后台,实时向前端推送数据。
metaor 安装
windows 安装 meteor
官方推荐 chocolatey 安装 meteor。
- 先从 chocolatey 安装 chocolatey
- 然后在命令行中运行
choco install meteor
但是 meteor 安装速度非常慢,一顿搜索之后,找到了 Windows 下通过二进制包安装的下载地址 https://install.meteor.com/windows,搜索来源 https://github.com/meteor/docs/blob/version-NEXT/long-form/alternate-windows-installation.md
OSX/Linux 安装 meteor
安装非常简单
curl https://install.meteor.com/ | sh
验证安装
命令行输入
meteor --version
输出版本号,表示安装成功
Meteor 1.8.1
mongodb 安装
windows 安装 mongodb
https://www.mongodb.com/ 下载安装包安装
OSX 安装 mongodb
brew install mongod
或者下载二进制包安装
是 mongod ,不是 mongodb
mongodb 图形界面
推荐 https://robomongo.org/, 易于使用,也是免费的。
meteor DDP
react,react-native 使用同一个 meteor 后台,所以 meteor 后台要与前端应用分开编写。
这就涉及到 meteor 中后台与前端的数据交互,meteor 中定义了一个 DDP协议。
DDP协议定义了 meteor 后台发布数据,客户端订阅数据的操作。
本应用使用已经编写好了的 DDP 协议库,地址如下: https://github.com/mondora/ddp.js。
创建 meteor 项目
meteor create --bare [project-name]
更多创建参数
meteor help create
meteor 连接 mongodb
meteor 项目启动命令如下:
meteor run
配置端口
meteor run --port 9090
meteor 连接自己的 mongodb
meteor 安装包中集成了 mongodb,默认启动的是集成的 mongodb。
为了连接自己的 mongodb,需要传入参数
MONGO_URL=mongodb://username:password@localhost:27017/[database-name]?authSource=admin meteor run --port 9090
刚开始没加上 authSource=admin 参数,一直连接不上 mongodb,加上之后就好了,根据需要加。
编写 meteor 后台
import {Meteor} from 'meteor/meteor';
// mongodb 的 todo collection
const Todo = new Meteor.Collection('todo');
// 发布数据,前端就可以调用
Meteor.publish('todo', () => {
return Todo.find();
});
/**
* 定义前端调用的方法
*/
Meteor.methods({
// 查找一条数据
getTodo(id) {
return Todo.findOne(id);
},
// 查找所有数据
getAllTodo() {
return Todo.find().fetch();
},
// 新增
addTodo(item) {
return Todo.insert(item);
},
// 删除
removeTodo(id) {
return Todo.remove({_id: id});
},
// 编辑
editTodo(item) {
return Todo.update({_id: item.id}, {$set: item});
},
/**
*
* @param {number 当前页面 从 1 开始} currentPage
* @param {number 单次请求总条数} pageSize
*/
getPageTodo(currentPage = 1, pageSize = 10) {
if (page < 1) {
return null;
}
// meteor 对 mongodb 的操作方法做了封装
// 更多操作请查看 meteor 官方文档
const total = Todo.find().count();
const list = Todo.find(
{},
{
skip: (currentPage - 1) * pageSize,
limit: pageSize,
}
).fetch();
return {total, data: list};
},
});
// 定义对 mongodb 的操作权限
// 若没有定义,则是允许所有增删改查操作
Todo.deny({
// 是否允许 mongodb 的新增操作, 返回 true 表示允许,否则不允许
insert() {
return true;
},
update() {
return true;
},
remove() {
return true;
},
});
export default Todo;
前端调用
定义高阶组件
为了代码复用,定义了高阶组件,react 与 react-native 可以共用
// meteor.js
import React, {Component} from 'react';
import DDP from 'ddp.js';
/**
* meteor 连接选项
*/
const meteorOptions = {
endpoint: 'ws://192.168.31.121:9090/websocket',// react-native 不支持 localhost,127.0.0.1,请替换为自己的 IPv4 地址
SocketConstructor: WebSocket,
reconnectInterval: 10000,// 重连间隔
autoConnect: true,// 是否自动连接
autoReconnect: true,// 是否自动重连
};
const PUBLIC_EVENTS = [
// 'ready',
// 'nosub',
'added',
'changed',
'removed',
// 'result',
// 'updated',
// 'error',
];
export default (WrapperComponent, {collectionName, methodName}) => {
class MeteorWrapper extends Component {
ddp = new DDP(meteorOptions);
lockRequest = false
recordSubscriptions = {};
state = {
meteorList: [],
initOver: false,
};
componentDidMount() {
if (!this.ddp) {
console.error(`数据推送未连接上服务器!`);
return;
}
// 添加订阅
this.addSubscription();
}
componentWillUnmount() {
// 取消订阅
this.removeSubscription();
// 断开连接
this.ddp.disconnect();
}
getDataResult() {
// 防止初始化请求次数过多
if (this.lockRequest) {
return
}
this.lockRequest = true
const {ddp} = this;
const self = this;
/**
* 调用后台定义的方法, 前端传递数组参数,meteor 后台接受到的是列表参数
*/
ddp.method(methodName, [1, 10]);
ddp.on('result', data => {
const {result} = data;
console.log(data);
self.setState({
meteorList: result,
initOver: true,
});
self.lockRequest = false
});
}
componentDidCatch(error, info) {
console.error(error, info);
}
addSubscription() {
if (!collectionName) {
console.error('mongodb collection 为空!');
return;
}
const {ddp} = this;
const self = this;
// 订阅数据
self.recordSubscriptions[collectionName] = ddp.sub(collectionName);
PUBLIC_EVENTS.forEach(event => {
ddp.on(event, () => {
console.log(event)
self.getDataResult();
});
});
ddp.on('error', error => {
console.error(`服务器推送数据错误,错误消息:${error}`)
});
ddp.on('ready', () => {
self.getDataResult();
});
}
removeSubscription() {
this.ddp.unsub(this.recordSubscriptions[collectionName]);
}
render() {
return <WrapperComponent {...this.props} {...this.state} />;
}
}
return MeteorWrapper;
};
react 使用示例
import React, {Component} from 'react';
import {List, Skeleton} from 'antd';
import './App.css';
import MeteorWrapper from './meteor'
function App(props) {
const {meteorList = [], initOver} = props
return (
<div className="App">
<List
itemLayout="horizontal"
dataSource={meteorList}
renderItem={item => (
<List.Item key={item.id}>
<Skeleton loading={!initOver} active avatar>
<List.Item.Meta
title={item.name}
description={item.desc}
/>
</Skeleton>
</List.Item>
)}
/>
</div>
);
}
export default MeteorWrapper(App, {
collectionName:'todo',
methodName:'getAllTodo'
})
react-native 使用示例
import React from 'react';
import {StyleSheet, Text, View, FlatList} from 'react-native';
import MeteorWrapper from './meteor'
function App(props) {
const {meteorList = [], initOver} = props
return (
<View style={styles.container}>
<FlatList
data={meteorList}
renderItem={({item}) => (
<View style={styles.item}>
<View style={styles.name}><Text>{item.name}</Text></View>
<View style={styles.desc}><Text>{item.desc}</Text></View>
</View>)}
/>
</View>
);
}
export default MeteorWrapper(App, {
collectionName:'todo',
methodName:'getAllTodo'
})
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
fontSize: 14,
lineHeight: 2,
},
item: {
padding: 10,
borderColor: '#ccc',
borderBottomWidth: 1,
borderStyle: 'solid',
},
name: {
color: '#000',
fontWeight: "900",
fontSize: 24
},
desc: {
color: '#666'
}
});
开启远程调试
运行命令
adb shell input keyevent 82
点击 dev setting
然后点击 Debug server host & port for device
设置为 127.0.0。1:8081
再次运行
adb shell input keyevent 82
点击 Debug js remote ,就会自动弹出调试页面。
IOS 运行报错
错误信息如下,请查看解决 React-Native mac10.14.4 运行报错 error Failed to build iOS project
error Failed to build iOS project. We ran "xcodebuild" command but it exited with error code 65. To debug build logs further, consider building your app with Xcode.app, by opening reactNative.xcodeproj
React 与 React-Native 使用同一个 meteor 后台的更多相关文章
- React 与 React Native 底层共识:React 是什么
此系列文章将整合我的 React 视频教程与 React Native 书籍中的精华部分,给大家介绍 React 与 React Native 结合学习的方法,此小节主要介绍 React 的底层原理与 ...
- 一次掌握 React 与 React Native 两个框架
此系列文章将整合我的 React 视频教程与 React Native 书籍中的精华部分,给大家介绍 React 与 React Native 结合学习的方法. 1. 软件开发语言与框架的学习本质 我 ...
- 用react-service做状态管理,适用于react、react native
转载自:https://blog.csdn.net/wo_shi_ma_nong/article/details/100713151 . react-service是一个非常简单的用来在react.r ...
- 小谈React、React Native、React Web
React有三个东西,React JS 前端Web框架,React Native 移动终端Hybrid框架,React Web是一个源码转换工具(React Native 转 Web,并之所以特别提出 ...
- React的React Native
React的React Native React无疑是今年最火的前端框架,github上的star直逼30,000,基于React的React Native的star也直逼20,000.有了React ...
- React Navigation & React Native & React Native Navigation
React Navigation & React Native & React Native Navigation React Navigation https://facebook. ...
- 《React Native 精解与实战》书籍连载「React 与 React Native 简介」
此文是我的出版书籍<React Native 精解与实战>连载分享,此书由机械工业出版社出版,书中详解了 React Native 框架底层原理.React Native 组件布局.组件与 ...
- React Navigation / React Native Navigation 多种类型的导航结合使用,构造合理回退栈
React Navigation 更新到版本5已经是非常完善的一套导航管理组件, 提供了Stack , Tab , Drawer 导航方式 , 那么我们应该怎样设计和组合应用他们来构建一个完美的回退栈 ...
- 重谈react优势——react技术栈回顾
react刚刚推出的时候,讲react优势搜索结果是几十页. 现在,react已经慢慢退火,该用用react技术栈的已经使用上,填过多少坑,加过多少班,血泪控诉也不下千文. 今天,再谈一遍react优 ...
随机推荐
- 点击<a>页面跳转解决办法/跨域请求,JSONP
有些时候做的东西刚好要用到链接,但又不需要去链接,只需要对onclick事件进行处理,但它却这样子写 <a href="#" onclick="gettext()& ...
- C++使用类和对象
1. 内置函数 程序调用函数时需要一定的时间和空间开销,其执行过程一般如下: 而C++提供了一种高效率的方法,即在编译的时候将所调用函数的代码直接嵌入到主函数中,而不是将流程转出去,这样可以避免函数调 ...
- 一支烟的时间导致他错失女神,Python查看撤回消息,力挽狂澜!
2011年1月21日 微信(WeChat) 是腾讯公司于2011年1月21日推出的一个为智能终端提供即时通讯服务的免费应用程序,由张小龙所带领的腾讯广州研发中心产品团队打造 .在互联网飞速发展的下.民 ...
- BZOJ_4892_[Tjoi2017]dna_哈希
BZOJ_4892_[Tjoi2017]dna_哈希 Description 加里敦大学的生物研究所,发现了决定人喜不喜欢吃藕的基因序列S,有这个序列的碱基序列就会表现出喜欢吃藕的 性状,但是研究人员 ...
- 磁盘IOPS计算与测量
IOPS (Input/Output Per Second)即每秒的输入输出量(或读写次数),是衡量磁盘性能的主要指标之一.IOPS是指单位时间内系统能处理的I/O请求数量,一般以每秒处理的I/O请求 ...
- CentOS7 安装Redis Cluster集群
上一篇中已经讲到了如何安装单击版Redis,这一篇我们来说下如何安装Cluster,关于哨兵模式这里我就不写文章安装了,有兴趣的同学可以自己去研究,哨兵模式可以在主从模式下在创建三台机器的哨兵集群监控 ...
- rand ----MATLAB (经典)
最近一直在学习matlab,我相信有一些同学已经发现,最近更新的关于matlab的内容比较多, 希望能够帮助到未来的小学弟学妹们! 永远爱你们的 ----新宝宝 rand 均匀分布的随机数全页折叠 语 ...
- jquery简易版xwPop.js弹出消息对话框插件
xwPop.js弹出消息对话框插件是一款含有多种情景模式的原生模态消息对话框代码,可用于替代浏览器默认的alert弹出对话框,它提供各种参数和方法,功能非常强大.目前已经在项目中有应用到xwpop开发 ...
- mysql获取表中日期的年月日时分秒
SELECT year(callTheRollTime) from schedule_account 获取年 SELECT month(callTheRollTime) from schedule_a ...
- python书籍推荐:python编码推荐(高清完整pdf)
目录INF-qa Python 编码规范................................................................................ ...