xsockets c#客户端发布
我想将消息从我的xamarin android客户端发布到我的服务器xsockets c#客户端发布,c#,android,xsockets.net,xsocket,C#,Android,Xsockets.net,Xsocket,我想将消息从我的xamarin android客户端发布到我的服务器 //connection return true var connection = await c.Open(); await c.Controller("mycontroller").Publish("chatmessage", new { Text = "Hello people!" }); 但是我的消息没有发送到服务器 在服务器上存在,但不进入方法 谢谢我刚刚添加了我的自定义别名 public void ChatM
//connection return true
var connection = await c.Open();
await c.Controller("mycontroller").Publish("chatmessage", new { Text = "Hello people!" });
但是我的消息没有发送到服务器
在服务器上存在,但不进入方法
谢谢我刚刚添加了我的自定义别名
public void ChatMessage(string message)
{
this.InvokeToAll(message, "chatmessage");
}
问题是您发送了一个带有属性文本的对象
[XSocketMetadata(PluginAlias = "videostream")]
但是,在服务器端,您希望消息是字符串
new {Text="Hello People!"}
因此,解决方案是发送您期望的内容(或在服务器端使用动态文件)
示例1-发送字符串
服务器:
客户:
示例2-发送对象
服务器:
客户:
完整代码示例
只需在安装了客户端和服务器的单个控制台应用程序中编写所有内容
Text:Hello World, Time:2017-06-02 07:50:40
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用XSockets.Core.XSocket.Helpers;
班级计划
{
静态void Main(字符串[]参数)
{
//启动服务器
Task.Run(()=>
{
使用(var server=XSockets.Plugin.Framework.Composable.GetExport())
{
server.Start();
Console.ReadLine();
}
});
//启动客户端
Task.Run(异步()=>
{
//只需等待以确保服务器已启动并正在运行
等待任务。延迟(5000);
var c=new XSockets.XSocketClient(“ws://localhost:4502,”http://localhost");
等待c.打开();
//从服务器发送时处理消息
c、 控制器(“聊天”).On(“消息”,(m)=>Console.WriteLine($“Text:{m.Text},Time:{m.Time}”);
//发送10条信息
对于(变量i=0;i<10;i++)
等待c.Controller(“chat”).Invoke(“message”,新消息{Text=“Hello World”,Time=DateTime.Now});
});
Console.ReadLine();
}
}
//聊天中使用的型号/信息
公共类消息
{
公共字符串文本{get;set;}
公共日期时间{get;set;}
}
//控制器
公共类聊天:XSockets.Core.XSocket.XSocketController
{
公共异步任务消息(消息m)
{
等待此消息。调用全部(m,“消息”);
}
}
public class Chat : XSockets.Core.XSocket.XSocketController
{
public async Task Message(string m)
{
// Broadcast... not very good... but it is just an example.
await this.InvokeToAll(m, "message");
}
}
// To get the message
c.Controller("chat").On<string>("message", (s) => Console.WriteLine(s));
await c.Controller("chat").Invoke("message","Hello World " + DateTime.Now.ToString());
Hello World 2017-06-02 07:44:14
public class Message
{
public string Text { get; set; }
public DateTime Time { get; set; }
}
public class Chat : XSockets.Core.XSocket.XSocketController
{
public async Task Message(Message m)
{
await this.InvokeToAll(m, "message");
}
}
c.Controller("chat").On<Message>("message", (m) => Console.WriteLine($"Text:{m.Text}, Time:{m.Time}"));
await c.Controller("chat").Invoke("message",new Message { Text = "Hello World", Time = DateTime.Now });
Text:Hello World, Time:2017-06-02 07:50:40
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XSockets.Core.XSocket.Helpers;
class Program
{
static void Main(string[] args)
{
// Start server
Task.Run(() =>
{
using (var server = XSockets.Plugin.Framework.Composable.GetExport<XSockets.Core.Common.Socket.IXSocketServerContainer>())
{
server.Start();
Console.ReadLine();
}
});
// Start client
Task.Run(async () =>
{
//Just wait to make sure the server is up and running
await Task.Delay(5000);
var c = new XSockets.XSocketClient("ws://localhost:4502", "http://localhost");
await c.Open();
// Handle message when sent from server
c.Controller("chat").On<Message>("message", (m) => Console.WriteLine($"Text:{m.Text}, Time:{m.Time}"));
// Send 10 messages
for (var i = 0; i < 10; i++)
await c.Controller("chat").Invoke("message", new Message { Text = "Hello World", Time = DateTime.Now });
});
Console.ReadLine();
}
}
// Model/Message used in chat
public class Message
{
public string Text { get; set; }
public DateTime Time { get; set; }
}
// Controller
public class Chat : XSockets.Core.XSocket.XSocketController
{
public async Task Message(Message m)
{
await this.InvokeToAll(m, "message");
}
}