xsockets c#client publish

时间:2017-05-31 10:06:00

标签: c# android xsockets.net xsocket

//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 在服务器上存在,但不进入方法

由于

2 个答案:

答案 0 :(得分:0)

我只是添加自定义别名

[XSocketMetadata(PluginAlias = "videostream")]

答案 1 :(得分:0)

问题是您发送了一个带有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");
    }
}