//connection return true
var connection = await c.Open();
await c.Controller("mycontroller").Publish("chatmessage", new { Text = "Hello people!" });
我想从我的xamarin android客户端发送消息到我的服务器
public void ChatMessage(string message)
{
this.InvokeToAll(message, "chatmessage");
}
但我的消息未发送到服务器
server console 在服务器上存在,但不进入方法
由于
答案 0 :(得分:0)
我只是添加自定义别名
[XSocketMetadata(PluginAlias = "videostream")]
答案 1 :(得分:0)
问题是您发送了一个带有Text
属性的对象new {Text="Hello People!"}
但是,在服务器端,您希望该消息是一个字符串
public void ChatMessage(string message)
{
this.InvokeToAll(message, "chatmessage");
}
所以解决方法是发送您期望的内容(或在服务器端使用动态)。
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");
}
}