一个client发送消息给orleans, 就只需要掉用Grain的函数就行了. 但是有时候Grain需要发送消息给client, 在orleans里面, 就只能通过Observer来实现.

 public interface IChat : IGrainObserver
{
void ReceiveMessage(string message);
} public class Chat : IChat
{
public void ReceiveMessage(string message)
{
Console.WriteLine(message);
}
} class HelloGrain : Grain, IHello
{
private ObserverSubscriptionManager<IChat> _subsManager;
public override async Task OnActivateAsync()
{
_subsManager = new ObserverSubscriptionManager<IChat>();
await base.OnActivateAsync();
}
public async Task Subscribe(IChat observer)
{
_subsManager.Subscribe(observer);
}
public async Task UnSubscribe(IChat observer)
{
_SubsManager.Unsubscribe(observer);
}
} public Task SendUpdateMessage(string message)
{
_SubsManager.Notify(s => s.ReceiveMessage(message));
return TaskDone.Done;
} //下面就是Grain发送消息给Client的代码
var friend = GrainClient.GrainFactory.GetGrain<IHello>();
Chat c = new Chat(); var obj = await GrainClient.GrainFactory.CreateObjectReference<IChat>(c);
await friend.Subscribe(obj);

有了上面的代码, 我们就可以按照自己的需求造一个广播出来.

  • 发送消息给Client上所有的人
  • 发送消息给Client上某一部分人
  • 发送消息给Client上某一个人
 enum DestType
{
DestType_All = ,
DestType_Server = ,
DestType_Player = ,
} //这是我们的观察者
public interface IGatewayObserver : IGrainObserver
{
void SendMessage(int destType, long dest, int msgid, byte[] buffer);
} public interface IAllGatewayGrain : IGrainWithIntegerKey
{
//注册网关
Task RegisterGateway(string key);
Task UnRegisterGateway(string key); //发送消息
Task SendMessage(int destType, long dest, int msgid, byte[] buffer); //注册观察者
Task RegisterObserver(string gateway, IGatewayObserver);
} public interface IGatewayGrain : IGrainWithStringKey
{
Task SendMessage(int destType, long dest, int msgid, byte[] buffer); Task RegisterObserver(string gateway, IGatewayObserver);
}

上面是接口的设计, 然后只需要在Client启动的时候, 把自己注册到两个Grain里面去, 然后其他的Grain就可以通过两个Grain来发送针对所有人, 服务器, 和个人的消息了.

orleans发送广播消息的更多相关文章

  1. udp发送广播消息

    import socket if __name__ == '__main__': # 创建udpsocket udp_socket = socket.socket(socket.AF_INET, so ...

  2. Linux--网络通信命令(给其它用户发送广播消息)

    1.命令名称:write 执行权限:所有用户  功能描述:向另外一个用户发送信息,以CTRL+D作为结束 语法:write <用户名>root向luxh用户发送信息[root@localh ...

  3. C#利用UdpClient发送广播消息

    首先写个接受消息的客户端.这里偷了点懒,new UdpClient(11000)就是用Udp方式侦听11000端口,侦听任何发送到11000端口的消息都会接收到. 代码 : ); Byte[] sen ...

  4. Linux下UDP收/发广播消息简单实现

    发送广播消息 #include<stdio.h> #include<stdlib.h> #include<string.h> #include<sys/typ ...

  5. Linux系统下UDP发送和接收广播消息小例子

    // 发送端 #include <iostream> #include <stdio.h> #include <sys/socket.h> #include < ...

  6. Unity3D笔记九 发送广播与消息、利用脚本控制游戏

    一.发送广播与消息 游戏对象之间发送的广播与消息分为三种:第一种向子对象发送,将发送至该对象的同辈对象或者子孙对象中:第二种为给自己发送,发送至自己本身对象:第三种为向父对象发送,发送至该对象的同辈或 ...

  7. 发送广播BroadcastReceiver

    import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.vi ...

  8. android在广播接收器BroadcastReceiver里面再进行发送广播,造成当前广播接收器不断循环执行问题

    最近在公司处理项目时,用到锁屏状态弹出activity进行提示,类似QQ消息弹屏提示的功能.当中用到了,假如该弹出activity已经位于锁屏界面外时,将不进行再次弹窗,而是发送广播进行通知数据更新, ...

  9. Android应用程序发送广播(sendBroadcast)的过程分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6744448 前面我们分析了Android应用程 ...

随机推荐

  1. Java 一些知识点总结

    本篇文章会对面试中常遇到的Java技术点进行全面深入的总结,帮助我们在面试中更加得心应手,不参加面试的同学也能够借此机会梳理一下自己的知识体系,进行查漏补缺(阅读本文需要有一定的Java基础).本文的 ...

  2. Linux中DNS的设置

    1.查看本机的域名 hostname 2.修改DNS 临时修改: hostname desktop0.example.com 永久修改: hostnamectl set-hostname deskto ...

  3. SQL Server 提供的各种数据访问接口

    在创建SQL Server的链接服务器时,可以看到有如下几种访问接口,其中我们常用的只有1.3.4.6.其中4是安装Oracle客户端才会出现的接口,3.6是由于我电脑上装了2008R2和2012两个 ...

  4. Windows Server 2016-Win Ser 2016已删减内容

    本章与大家简单聊聊已经从Windows Server 2016产品中删减掉或者计划在以后潜在删除的功能或者模块. 文件服务器 共享和存储管理贴靠-Microsoft 管理控制台已删除. 相反,请执行以 ...

  5. java操作elasticsearch实现聚合查询

    1.max 最大值 //max 求最大值 @Test public void test30() throws UnknownHostException{ //1.指定es集群 cluster.name ...

  6. ESLint笔记

    ESLint是JavaScript的代码检查工具.因为JS是弱类型的语言,不需要编译,代码错误是在运行时调适的,所以需要个工具在编码的过程发现问题.ESLint的初衷是为了让程序员可以创建自己的检测规 ...

  7. ZooKeeper Observers解决节点过多时写性能下降问题

    ZooKeeper Observers Observers: Scaling ZooKeeper Without Hurting Write Performance How to use Observ ...

  8. Linux 27 岁了!盘点 Linux 的 27 件趣事

    Linux 27 岁了!盘点 Linux 的 27 件趣事 许多人认为10月5日是 Linux 系统的周年纪念日,因为这是 Linux 在1991年首次对外公布的时间.不过,你可能不知道的是,早在19 ...

  9. 鼠标右键打开命令行cmd(管理员身份)

    参考:https://blog.csdn.net/bdss58/article/details/54745380 添加到注册表 将下面命令保存为reg文件: Windows Registry Edit ...

  10. 洛谷P1127-词链

    Problem 洛谷P1127-词链 Accept: 256    Submit: 1.3kTime Limit: 1000 mSec    Memory Limit : 128MB Problem ...