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

2 Answers2

1

The issue is that you sent a object with the property Text

new {Text="Hello People!"}

However, on the server side you expect the message to be a string

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

So the fix is to send what you expect (or use a dynamic on the server side).

Example 1 - Sending strings

Server:

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");
    }
}

Client:

// 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());

Result:

Hello World 2017-06-02 07:44:14

Example 2 - Sending object

Server:

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");
    }
}

Client:

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 });

Result

Text:Hello World, Time:2017-06-02 07:50:40

Complete Code Sample

Just wrote everything in a single console application with both client and server installed.

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");
    }
}
Uffe
  • 2,275
  • 1
  • 13
  • 9
0

I just add my custom alias

[XSocketMetadata(PluginAlias = "videostream")]