gpt4 book ai didi

c# - 如何在 XSockets 中使用 C# 客户端 API 获取/设置属性

转载 作者:行者123 更新时间:2023-12-01 16:26:52 26 4
gpt4 key购买 nike

在 XSockets 服务器 API 中,有一个有关如何使用 JavaScript API 获取/设置服务器 Controller 上的属性的示例。

Get/Set properties from the client APITop

If you have a property with a public getter or setter you can access the getter/setter methods from the client API’s

public string MyProp {get;set;}

The property above can be retrieved and changed from the client API’s (both JavaScript and C#). Example on how to set a new value from JavaScript

conn.publish('set_MyProp',{value:'NewValue'});

See the client API’s for more information.

但是 Client API's page 上没有任何信息

我很难弄清楚 JavaScript 代码的等效 C# 客户端代码是什么 conn.publish('set_MyProp',{value:'NewValue'});

非常感谢任何帮助。

最佳答案

好吧,我通过尝试错误发现了困难的方法:

Client.Send(new { value = "NewValue" }, "set_MyProp");

相当于代码:

conn.publish('set_MyProp',{value:'NewValue'});

注意“value”的大小写!!!不要将单词大写!

更新

我创建了两个扩展方法,这使得获取和设置属性值变得非常容易(还有一个 WaitForConnection ,它在单元测试等同步场景中很有用)。

由于 XSockets(非常不幸)不是开源的,而且文档也很少,我必须猜测事情是如何工作的,所以我的扩展方法可能不会像我能够阅读源代码那样高效和优雅代码,我对从服务器读取属性的方法可能有点过于“小心”...如果有人知道如何改进它,请编辑答案或在评论部分提出建议:

public static class XSocketClientExtensions
{
public static bool WaitForConnection(this XSocketClient client, int timeout=-1) {
return SpinWait.SpinUntil(() => client.IsConnected, timeout);
}

public static void SetServerProperty(this XSocketClient client, string propertyName, object value) {
client.Send(new { value = value }, "set_" + propertyName);
}

public static string GetServerProperty(this XSocketClient client, string propertyName) {
var bindingName = "get_" + propertyName;
// why event name is lowercase?
var eventName = bindingName.ToLowerInvariant();
// we must be careful to preserve any existing binding on the server property
var currentBinding = client.GetBindings().FirstOrDefault(b => b.Event == eventName);
try {
// only one binding at a time per event in the client
if (currentBinding != null)
client.UnBind(bindingName);
var waitEvent = new ManualResetEventSlim();
string value = null;
try {
client.Bind(bindingName, (e) => {
value = e.data;
waitEvent.Set();
});
// we must "Trigger" the reading of the property thru its "event" (get_XXX)
client.Trigger(bindingName);
// and wait for it to arrive in the callback
if (waitEvent.Wait(5000))
return value;
throw new Exception("Timeout getting property from XSockets controller at " + client.Url);
} finally {
client.UnBind(bindingName);
}
} finally {
// if there was a binding already on the "property getter", we must add it back
if (currentBinding != null)
client.Bind(bindingName, currentBinding.Callback);
}
}
}

使用非常简单:

// Custom controller
public class MyController : XSocketController
{
public int Age { get; set; }

public override void OnMessage(ITextArgs textArgs) {
this.SendToAll(textArgs);
}
}

// then in the client
var client = new XSocketClientEx("ws://127.0.0.1:4502/MyController", "*");
client.WaitForConnection(); // waits efficiently for client.IsConnected == true
client.SetServerProperty("Age", 15);
int age = Convert.ToInt32(client.GetServerProperty("Age"));

您可以跳过以下内容,这只是一个咆哮!

有些事情使得从一开始就很难确定下来。 JavaScript 客户端和 C# 客户端之间没有达成一致。因此,你在一种技术中学到的东西并不能转化为另一种技术。在我自己的多语言客户端 API 上,我尝试使所有 API 的行为和外观非常相似(即使不相同),因此代码几乎是可移植的。我有一个在 JavaScript、C# 和 Java 中看起来几乎相同的 API。

令我困扰的差异是:

  1. 这些方法具有不同的名称:JavaScript 中的 publish 与 C# 中的 Send
  2. 参数以相反的顺序传递:C# 中的值、事件,JavaScript 中的事件、值
  3. 本身没有区别,但如果您在 C# 示例中使用“Value”,它将不起作用,您必须使用“value”...我不认为它应该区分大小写。 .. 这个理由很有说服力。那我就不再狡辩了!

关于c# - 如何在 XSockets 中使用 C# 客户端 API 获取/设置属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23717272/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com