[置顶] Xamarin android中使用signalr实现即时通讯
前面几天也写了一些signalr的例子,不过都是在Web端,今天我就来实践一下如何在xamarin android中使用signalr,刚好工作中也用到了这个,也算是总结一下学到的东西吧,希望能帮助你们,更快地熟悉使用xamarin android进行即时通讯。
先来看一下最终实现的效果:
这个简单的例子主要分为两部分:
1.一个Signalr web端提供访问的地址,也就是前面所写的例子 MVC中使用signalR入门教程。发布这个web网页,为Xamarin android的signalr客户端 提供一个url地址连接
2. xamarin android 中引入signalr.client 库,通过连接web地址发送消息,接收消息。
1.发布web网站提供地址
2.新建xamarin android项目引入Signalr.Client 库
Exception while loading assemblies: System.IO.FileNotFoundException: Could not load assembly 'System.Net.Http.Extensions, Version=1.5.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Perhaps it doesn't exist in the Mono for Android profile?
文件名:“System.Net.Http.Extensions.dll”
在 Xamarin.Android.Tuner.DirectoryAssemblyResolver.Resolve(AssemblyNameReference reference, ReaderParameters parameters)
在 Xamarin.Android.Tasks.ResolveAssemblies.AddAssemblyReferences(ICollection`1 assemblies, AssemblyDefinition assembly, Boolean topLevel)
在 Xamarin.Android.Tasks.ResolveAssemblies.Execute()
这个错误说明signalr.client 在xamarin android支持的还不够好,今天厂里的牛哥说是nuget上面引入库不兼容,所以只能在xamarin官网上下载后引入。如果你们也是这样的错误,可以下载这个示例xamarin
signalr入门例子,引入里面的signalr.client 库。开发xamarin android项目的时候总会碰到很多小错误,虽然不是什么大bug,但是目前圈子很小,百度能解决的不是很多,有基础的话逛逛StackOverflow 还是不错的选择。有些小错误,能真是能让你喝一壶的.....,
引入好signalr.client 之后,新建一个类SignalrChatClient这是这个即时通讯的桥梁,as shown in the figure
3.SignalrChatClient负责创建、发送消息、接受消息的方法
SignalrChatClient.cs,代码如下
using System;
using System.Threading.Tasks;
using Microsoft.AspNet.SignalR.Client;
namespace SignalrClientDemo
{
public class SignalrChatClient
{
private readonly HubConnection _connection;
private readonly IHubProxy _proxy;//客户端代理服务器端中心
public event EventHandler<string> OnReceiveEvent; //定义一个接收server端的事件
public SignalrChatClient()
{
_connection = new HubConnection($"http://192.168.16.137:400");
_proxy = _connection.CreateHubProxy("serverHub");
}
//负责连接的方法
public async Task Connect()
{
await _connection.Start();
_proxy.On("showMessage", (string platform,string msg) =>
{
OnReceiveEvent(this,platform+":" +msg);
});
}
public Task Send(string message)
{
string serverMethod = "sendMsg"; //serverHub中定义的server端方法名称
if (_connection.State != ConnectionState.Connected)
{
Console.WriteLine("未连接");
return null;
}
Console.WriteLine("已连接");
return _proxy.Invoke(serverMethod,message);//Invode the 'SendMessage' method on ther server
}
}
}
4.最后一步Activity的布局和使用SignalrChatClient
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/et_msg"
android:layout_width="match_parent"
android:textColor="#ff0000"
android:layout_height="50dp" />
<Button
android:id="@+id/MyButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/Hello" />
<ListView
android:id="@+id/lv_msg"
android:layout_width="match_parent"
android:layout_height="30dp" />
</LinearLayout>
MainActivity.cs
using Android.App;
using Android.Widget;
using Android.OS;
namespace SignalrClientDemo
{
[Activity(Label = "SignalrClientDemo", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
int count = 1;
protected override async void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
Button button = FindViewById<Button>(Resource.Id.MyButton);
EditText et_msg = FindViewById<EditText>(Resource.Id.et_msg);
ListView lv_msg = FindViewById<ListView>(Resource.Id.lv_msg);
SignalrChatClient client = new SignalrChatClient();
await client.Connect();
var adapter = new ArrayAdapter(this,Android.Resource.Layout.SimpleListItem1);
string msg = et_msg.Text;
//发送消息
button.Click += delegate {
client.Send(msg);
et_msg.Text = string.Empty;
};
lv_msg.Adapter = adapter;
//使用委托接收消息
client.OnReceiveEvent += (sender, message) => RunOnUiThread(() =>
{
adapter.Add(message);
});
}
}
}
实现了这个效果聊天的效果之后,会发现这xamarin android客户端使用signalr实现即时通讯的方式和mvc里的差不多,方式其实是一样的,客户端发送的消息的事件也是要调用服务器端的serverHub类中方法sendMsg,接收消息同样是在委托里定义服务器端中声明的客户端方法showMessage.原理很相似。最近工作之余不是很忙,准备写一个聊天的xamarin android项目出来,希望能快点做出来吧。
例子下载地址:xamarin signalr入门例子
作者:张林
标题:Xamarin android中使用signalr实现即时通讯 原文地址:http://blog.csdn.net/kebi007/article/details/53544379
转载随意注明出处
[置顶] Xamarin android中使用signalr实现即时通讯的更多相关文章
- [置顶]
xamarin android使用gps定位获取经纬度
看了文章你会得出以下几个结论 1.android定位主要有四种方式GPS,Network(wifi定位.基站定位),AGPS定位 2.绝大部分android国产手机使用network进行定位是没有作用 ...
- [置顶]
xamarin android使用zxing扫描二维码
好久没写了,这片文章篇幅不长,概述一下在xamarin android中用 ZXing.Net.Mobile库扫描二维码读取url的示例.扫码支付,扫码登录,App上各种各样的扫码,好像没个扫码的就有 ...
- [置顶]
Xamarin android如何调用百度地图入门示例(一)
在Xamarin android如何调用百度地图呢? 首先我们要区分清楚,百度地图这是一个广泛的概念,很多刚刚接触这个名词"百度地图api",的确是泛泛而谈,我们来看一下百度地图的 ...
- [置顶]
xamarin android toolbar(踩坑完全入门详解)
网上关于toolbar的教程有很多,很多新手,在使用toolbar的时候踩坑实在太多了,不好好总结一下,实在浪费.如果你想学习toolbar,你肯定会去去搜索androd toolbar,既然你能看到 ...
- [置顶]
xamarin android自定义标题栏(自定义属性、回调事件)
自定义控件的基本要求 这篇文章就当是自定义控件入门,看了几篇android关于自定义控件的文章,了解了一下,android自定义控件主要有3种方式: 自绘控件:继承View类,所展示的内容在OnDra ...
- [置顶]
xamarin android自定义spinner
以前弄的一个下拉框时自带的spinner,感觉好丑,实际效果实在满足不了基本的UI界面要求,还是自己动手丰衣足食,看了网上关于android中自定义spinner的文章,感觉实现原理还是比较简单,所以 ...
- [置顶]
xamarin android 布局尺寸了解
为了使UI界面在不同大小的移动端显示器上能够正常显示,大家可能都知道使用sp作为字体大小的单位,dp作为其他元素长度的单位. 前几天看了一篇文章关于 App设计规范的,文章用心写的非常好,这里是链接 ...
- [置顶]
xamarin android Fragment实现底部导航栏
前段时间写了篇关于Fragment的文章,介绍了基础的概念,用静态和动态的方式加载Fragment Xamarin Android Fragment的两种加载方式.下面的这个例子介绍xamarin ...
- [置顶]
Xamarin Android安装教程(2016最新亲测安装版)
写这篇安装教程前要说的几句话 之前很多人想用Vs来开发Android项目,苦于这个环境的安装.的确这并不是一件简单的事情,并不是开发者都能在花一上午能装好,如果你花了一天时间,第一个Xamarin ...
随机推荐
- 撸起袖子加油干 golang入坑系列
还是提醒一下,里面有段子,不都是技术. 冲着技术来的,慢走不送.没有版权,但可以给我发邮件(ztao8607@gmail.com) 在我的发小朋友中,终于最后一位打光棍的要结婚了. 说实话,真心不容易 ...
- bootstrap_table_class表格样式实例
<div class="container"> <h2>表格</h2> <!--table标签级别的样式说明如下: ----.table ...
- css选择器的优先级问题
当我们写页面的时候,不知道你会不会产生这样的问题,为什么我给他添加的这条样式分明已经选择到我要给的元素了,但是他的样式并没有生效,那是为什么呢? 定义的属性有冲突时,浏览器会选择用那一套样式呢,下面来 ...
- JSP8
一.EL表达式 JSP表达式语言(EL)使得访问存储在JavaBean中的数据变得非常简单.JSP EL既可以用来创建算术表达式也可以用来创建逻辑表达式.在JSP EL表达式内可以使用整型数,浮点数 ...
- ThinkPHP中的Model模型
一 实例化模型对象 ①实例化通用模型 例:$goods_model = new \Model\GoodsModel(); $goods_Model = D('Goods'); ②实例化基例模型 ...
- 判断是否AVL平衡二叉书
#include<iostream> #include<vector> #include<stack> #include<string> #includ ...
- ssm开发关于web.xml配置
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" ...
- MATLAB中最基本函数plot()的用法
1二维平面图形 1.1基本图形函数 画出一条正弦曲线和一条余弦曲线 1.1.1绘图参数表 y 黄- 实线. 点< 小于号 m 紫: 点线o 圆s 正方形 c 青-. 点划线x 叉号d 菱形 r ...
- Java SE 8 流库(二)
1.3. filter,map,flatMAP方法 流的转换会产生一个新流,它的元素派生出自另一个流中的元素: Stream<T> filter(Predicate<? super ...
- quickhybrid】如何实现一个Hybrid框架
章节目录 [quickhybrid]如何实现一个跨平台Hybrid框架 [quick hybrid]架构一个Hybrid框架 [quick hybrid]H5和Native交互原理 [quick hy ...