想了解Minrouter特点预览前后端通用路由的新动态吗?本文将为您提供详细的信息,我们还将为您解答关于前端路由的两种模式的相关问题,此外,我们还将为您介绍关于064——VUE中vue-router之
想了解Minrouter 特点预览 前后端通用路由的新动态吗?本文将为您提供详细的信息,我们还将为您解答关于前端路由的两种模式的相关问题,此外,我们还将为您介绍关于064——VUE中vue-router之使用路由别名定制(alias)、Akka路由_RoundRobinRoutingLogic、C++ protobuffer 前后端通信 简单应用、Flutter 路由问题找不到路由RouteSetting的新知识。
本文目录一览:- Minrouter 特点预览 前后端通用路由(前端路由的两种模式)
- 064——VUE中vue-router之使用路由别名定制(alias)
- Akka路由_RoundRobinRoutingLogic
- C++ protobuffer 前后端通信 简单应用
- Flutter 路由问题找不到路由RouteSetting
Minrouter 特点预览 前后端通用路由(前端路由的两种模式)
Minrouter 特点预览 介绍
非常精简的一个前后端通用路由,API 风格像 Express,兼容 Express、Koa 后端,同时也可集成
React、Vue、Preact、dot..js 等前端框架模板引擎,库非常小,gzip 后 1k 左右,源码很容易读懂。
特点
-
支持 koa
-
支持 express
-
支持浏览器 pushState&replaceState
-
中间件路由器,API 如 express
预览
Minrouter 特点预览 官网
https://github.com/cheft/minrouter
064——VUE中vue-router之使用路由别名定制(alias)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>vue-router之使用路由别名定制(alias)</title>
<script src="vue.js"></script>
<script src="node_modules/vue-router/dist/vue-router.js"></script>
</head>
<body>
<div id="demo">
<router-link to="/">首页</router-link>
<router-link to="/about">关于我们</router-link>
<router-view></router-view>
</div>
<script type="text/x-template" id="home">
<div>
<li v-for="v in news">
<a href="#" @click.prevent="go(v.id)">{{v.title}}</a>
</li>
</div>
</script>
<script type="text/x-template" id="content">
<div>
<h1>{{field.title}}--{{field.id}}</h1>
<p>
{{field.content}}
</p>
<a href="" @click.prevent="back()">返回首页</a>
</div>
</script>
<script>
var data = [
{id: 1, title: "php课程", content: "php是个比较牛的技术"},
{id: 2, title: "java课程", content: "java是个比较牛的技术"},
{id: 3, title: "pathon课程", content: "pathon是个比较牛的技术"}
]
const home = {
template: "#home",
data() {
return {
news: data
}
},
methods:{
go(id){
//var url=''/content/''+id; //url第一种写法
//var url={name:''content'',params:{id:id}};//url第二种写法
//this.$router.replace(url);//replace()替换历史记录中的跳转:
var url={''path'':''/content/''+id};//跳转到详情页 //url第三种写法
this.$router.push(url);
}
}
}
const content = {
template: "#content",
data() {
return {
field: {}
}
},
mounted() {
var id = this.$route.params.id;
for (let k = 0; k < data.length; k++) {
if (data[k].id == id) {
this.field = data[k];
}
}
},
methods:{
back(){
this.$router.go(-1);
}
}
}
let routes = [
{path: ''/'', component: home},
{path: ''/content/:id'', component: content, name: "content"},
{path: ''/content/3'',alias:[''/about'']}
];
//把组件交给路由器:
let router = new VueRouter({routes});
new Vue({
el: "#demo",
router
});
</script>
</body>
</html>
Akka路由_RoundRobinRoutingLogic
Akka路由_RoundRobinRoutingLogic
使用Round Robin算法的Router,代码中有注释,基本和上篇文章中的代码一样
http://my.oschina.net/xinxingegeya/blog/369721,
具体如下,关于Round Robin,请移步,http://my.oschina.net/xinxingegeya/blog/369781
下面是主要代码,
package com.usoft10;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.routing.ActorRefRoutee;
import akka.routing.CustomRouterConfig;
import akka.routing.RoundRobinRoutingLogic;
import akka.routing.Routee;
import akka.routing.Router;
import java.util.ArrayList;
import java.util.List;
public class BurstyMessageRouter extends CustomRouterConfig {
private int noOfInstances;
public BurstyMessageRouter(int inNoOfInstances) {
noOfInstances = inNoOfInstances;
}
@Override
public Router createRouter(ActorSystem system) {
final List<Routee> routees = new ArrayList<Routee>(noOfInstances);
for (int i = 0; i < noOfInstances; i++) {
routees.add(new ActorRefRoutee(system.actorOf(
Props.create(MsgEchoActor.class), "actor-" + String.valueOf(i))));
}
/**
* 使用轮询调度算法的router
*/
return new Router(new RoundRobinRoutingLogic(), routees);
}
}
启动router,发出消息,
package com.usoft10;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
public class Example {
/**
* @param args
*/
public static void main(String[] args) throws InterruptedException {
ActorSystem _system = ActorSystem.create("CustomRouterExample");
ActorRef burstyMessageRouter = _system.actorOf(Props.create(
MsgEchoActor.class).withRouter(new BurstyMessageRouter(5)), "MsgEchoActor");
/**
* 在这里模拟发出10个消息,使用RoundRobinRoutingLogic的router会轮询调度每个actor接收消息
* 也就是说每次tell,router只会选择其中一个actor进行消息的响应
*/
for (int i = 1; i <= 10; i++) {
//sends series of messages in a round robin way to all the actors
burstyMessageRouter.tell("are you ready?" + String.valueOf(i), ActorRef.noSender());
}
Thread.sleep(2000);
_system.shutdown();
}
}
运行结果,
[INFO] [01/20/2015 16:08:24.668] [main] [Remoting] Starting remoting
[INFO] [01/20/2015 16:08:25.242] [main] [Remoting] Remoting started; listening on addresses :[akka.tcp://CustomRouterExample@127.0.0.1 :2552]
[INFO] [01/20/2015 16:08:25.246] [main] [Remoting] Remoting now listens on addresses: [akka.tcp://CustomRouterExample@127.0.0.1 :2552]
Received Message ''are you ready?1'' in Actor akka://CustomRouterExample/user/actor-0
Received Message ''are you ready?6'' in Actor akka://CustomRouterExample/user/actor-0
Received Message ''are you ready?2'' in Actor akka://CustomRouterExample/user/actor-1
Received Message ''are you ready?7'' in Actor akka://CustomRouterExample/user/actor-1
Received Message ''are you ready?3'' in Actor akka://CustomRouterExample/user/actor-2
Received Message ''are you ready?8'' in Actor akka://CustomRouterExample/user/actor-2
Received Message ''are you ready?4'' in Actor akka://CustomRouterExample/user/actor-3
Received Message ''are you ready?9'' in Actor akka://CustomRouterExample/user/actor-3
Received Message ''are you ready?5'' in Actor akka://CustomRouterExample/user/actor-4
Received Message ''are you ready?10'' in Actor akka://CustomRouterExample/user/actor-4
[INFO] [01/20/2015 16:08:27.294] [CustomRouterExample-akka.remote.default-remote-dispatcher-8] [akka.tcp://CustomRouterExample@127.0.0.1 :2552/system/remoting-terminator] Shutting down remote daemon.
[INFO] [01/20/2015 16:08:27.297] [CustomRouterExample-akka.remote.default-remote-dispatcher-8] [akka.tcp://CustomRouterExample@127.0.0.1 :2552/system/remoting-terminator] Remote daemon shut down; proceeding with flushing remote transports.
[INFO] [01/20/2015 16:08:27.348] [ForkJoinPool-3-worker-7] [Remoting] Remoting shut down
[INFO] [01/20/2015 16:08:27.348] [CustomRouterExample-akka.remote.default-remote-dispatcher-7] [akka.tcp://CustomRouterExample@127.0.0.1 :2552/system/remoting-terminator] Remoting shut down.
Process finished with exit code 0
================END================
C++ protobuffer 前后端通信 简单应用
后端发送多个protobuffer消息到前端,前端用socket监听,如何区分消息类型呢?
//定义心跳包
DseHeartbeat _DseHeartbeat;
DseHeartbeat _DseHeartbeat1;
_DseHeartbeat.set_time( 1544432682 );
char a[1000];
memset ( a ,0,1000);
std::string str;
_DseHeartbeat.SerializeToString( &str );
memcpy(a,str.c_str(),str.length());
_DseHeartbeat1.ParseFromString(str);
定义了一个proto,设置int字段为当前的时间戳,可以看到被反序列化成了以上内容。
可以在string的前面添加两个字段,1个是4位的消息长度,另一个是4位的消息类型。
1 enum S2C_EVENT
2 {
3 C2S_DceLimitTank = 4215 , //DceLimitTank
4 S2C_DseUserData = 770 , //DseUserData
5 S2C_DseLimitTank = 669,
6 S2C_DseActivityData = 516 ,
7 S2C_DseHeartbeat = 660 ,
8 };
定义几个消息的类型。
客户端在解析的时候:
int iostring_readInt ( const char* d ,int offset ) {
int ret = 0;
d += offset;
ret = (*d & 0xff) << 24; d++;
ret += (*d & 0xff) << 16; d++;
ret += (*d & 0xff) << 8; d++;
ret += *d & 0xff;
return ret;
}
void handleData ( int size ) {
int index = 0;
while (index<size) {
int headlen = sizeof ( unsigned short )+ sizeof ( unsigned int );
int len = iostring_readInt ( buf+ index );
int pkgType = iostring_readInt ( buf + index ,sizeof ( unsigned int ) );
int requireLen = len - sizeof ( unsigned int );
if (requireLen> 0 )
{
char* response = new char[requireLen];
memcpy ( response ,buf+index + 8 ,requireLen );
std::string str2;
str2.assign ( response ,requireLen );
switch (pkgType) {
case S2C_DseHeartbeat:
qDebug () << _DseHeartbeat.ParseFromString ( str2 );
qDebug ( ("心跳包" + std::to_string ( _DseHeartbeat.time () )).c_str () );
break;
case S2C_DseActivityData:
qDebug () << _DseActivityData.ParseFromString ( str2 );
qDebug ( ("活动:oil " + std::to_string ( _DseActivityData.oiltime () )).c_str () );
break;
case S2C_DseUserData:
qDebug () << _DseUserData.ParseFromString ( str2 );
qDebug ( ("玩家名" + _DseUserData.name () +" level:" +std::to_string ( _DseUserData.level () )).c_str () );
break;
case S2C_DseLimitTank:
_DseLimitTank.ParseFromString ( str2 );
_DropList = _DseLimitTank.maindrop ();
for (const DropData& _DropData1 : _DropList.droplist ()) {
qDebug () << (("droplist: type:" + std::to_string ( _DropData1.type () ) + " id:" + std::to_string ( _DropData1.id () ))).c_str ();
}
qDebug () << QString ( "错误码 %1" ).arg ( _DseLimitTank.res () );
default:
break;
}
}
index = index+len+4;
}
}
因为客户端在recv的时候,可能一次读取多条消息,所以每次读取前四位(该消息的长度)->下四位(消息类型)->消息的内容->把消息ParseFromString 反序列化。
在这里反序列化失败过几次,因为protobuf里面还有''\0'',所以,如果把char*直接赋值给string,\0会被丢弃。所以上面代码中memcpy ,assign都会保留\0。反序列化成功。
Flutter 路由问题找不到路由RouteSetting
如何解决Flutter 路由问题找不到路由RouteSetting?
有一个大问题,我与它斗争了 2 个小时,但仍然没有创建解决方案,所以我来到这里寻求帮助,昨天当我打开我的应用程序时,一切都很好,今天我改变了一点咬我的代码,但我什至尝试删除它以检查它是否有问题,当我删除它时它仍然很糟糕。这是我在终端的问题
处理手势时抛出以下断言: 找不到路线 RouteSettings("/Category-meals",{id: c1,title: Italian}) 的生成器 _WidgetsAppState。 确保您的根应用小部件提供了一种生成 这条路线。 按以下顺序搜索路线生成器:
对于“/”路由,使用“home”属性(如果非空)。 否则,如果“路由”表具有路由条目,则使用“路由”表。 否则,将调用 onGenerateRoute。它应该为任何有效路由返回一个非空值 由“家”和“路线”处理。 最后,如果所有其他方法都失败,则调用 onUnkNownRoute。 不幸的是,没有设置 onUnkNownRoute。 这是我使用路由命令的文件,
main.dart
`import ''package:Flutter/material.dart'';
import ''./category_meals.dart'';
import ''categories_screen.dart'';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: ''DeliMeals'',theme: ThemeData(
primarySwatch: Colors.pink,accentColor: Colors.Amber,canvasColor: Color.fromrGBO(255,254,229,1),textTheme: ThemeData.light().textTheme.copyWith(
bodyText1: TextStyle(
color: Color.fromrGBO(20,51,),bodyText2: TextStyle(
color: Color.fromrGBO(20,headline6: TextStyle(
fontSize: 24,)),home: CategoriesScreen(),routes: {
CategoryMeals.routeName: (ctx) => CategoryMeals(),},);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(''DeliMeals''),body: Center(
child: Text(''Navigation Time!''),);
}
}
`
category-meals.dart
`import ''package:Flutter/material.dart'';
import ''./dummy_data.dart'';
class CategoryMeals extends StatelessWidget {
static const routeName = ''Category-meals'';
/* final String categoryId;
final String categoryTitle;
CategoryMeals(this.categoryId,this.categoryTitle); */
@override
Widget build(BuildContext context) {
final routesArgs = ModalRoute.of(context).settings.arguments as Map<String,String>;
final categoryTitle = routesArgs[''title''];
final categoryId = routesArgs[''id''];
final categoryMeals = DUMMY_MEALS.where((meal) {
return meal.categories.contains(categoryId);
}).toList();
return Scaffold(
appBar: AppBar(
title: Text(categoryTitle),body: ListView.builder(itemBuilder: (ctx,index) {
return Text(categoryMeals[index].title);
},itemCount: categoryMeals.length,);
}
}
`
category-items.dart
`import ''package:Flutter/material.dart'';
import ''./category_meals.dart'';
class CategoryItem extends StatelessWidget {
final String id;
final String title;
final Color color;
CategoryItem(this.id,this.title,this.color);
void selectCategory(BuildContext ctx) {
Navigator.of(ctx).pushNamed(''/Category-meals'',arguments: {
''id'': id,''title'': title
});
}
@override
Widget build(BuildContext context) {
return InkWell(
onTap: () => selectCategory(context),splashColor: Theme.of(context).primaryColor,borderRadius: BorderRadius.circular(15),child: Container(
padding: const EdgeInsets.all(15),child: Text(
title,style: Theme.of(context).textTheme.headline6,decoration: Boxdecoration(
gradient: LinearGradient(
colors: [
color.withOpacity(0.7),color,],begin: Alignment.topLeft,end: Alignment.bottomright,);
}
}
如果有人知道解决方案,我会很乐意提供帮助,并解释出了什么问题,谢谢
解决方法
您在 category-meals.dart 中有一个 routeName = ''Category-meals''
,而在 category-items.dart 中,您正试图浏览不存在的 routeName = ''/Category-meals''
。尝试将 category-items.dart 中的“Category-meals”更改为“/Category-meals”,应该可以。
我们今天的关于Minrouter 特点预览 前后端通用路由和前端路由的两种模式的分享已经告一段落,感谢您的关注,如果您想了解更多关于064——VUE中vue-router之使用路由别名定制(alias)、Akka路由_RoundRobinRoutingLogic、C++ protobuffer 前后端通信 简单应用、Flutter 路由问题找不到路由RouteSetting的相关信息,请在本站查询。
本文标签: