1
//connection return true
var connection = await c.Open();

await c.Controller("mycontroller").Publish("chatmessage", new { Text = "Hello people!" });

I want publish message from my xamarin android client, to my server

public void ChatMessage(string message)
{
    this.InvokeToAll(message, "chatmessage");
}

but my message is not sent to the server

server console on server is exist, but does not go in method

Thanks

4

2 回答 2

1

问题是您发送了一个带有属性 Text 的对象

new {Text="Hello People!"}

但是,在服务器端,您希望消息是一个字符串

public void ChatMessage(string message)
{
    this.InvokeToAll(message, "chatmessage");
}

因此,解决方法是发送您期望的内容(或在服务器端使用动态)。

示例 1 - 发送字符串

服务器:

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

示例 2 - 发送对象

服务器:

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");
    }
}
于 2017-06-02T05:54:28.400 回答
0

我只是添加我的自定义别名

[XSocketMetadata(PluginAlias = "videostream")]
于 2017-06-01T14:37:22.880 回答