【问题标题】:xsockets c# client publishxsockets c#客户端发布
【发布时间】:2017-11-01 01:57:00
【问题描述】:
//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 服务器上存在,但不进入方法

谢谢

【问题讨论】:

    标签: c# android xsockets.net xsocket


    【解决方案1】:

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

    [XSocketMetadata(PluginAlias = "videostream")]
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-02
        • 1970-01-01
        • 2015-12-17
        • 2017-02-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多