在XSockets Server API中,有一个示例,说明如何使用JavaScript API在服务器控制器上获取/设置属性。
从客户端API获取/设置属性
如果您拥有带有公共getter或setter的属性,则可以从客户端API的getter / setter方法访问
public string MyProp {get;set;}
可以从客户端API(JavaScript和C#)中检索和更改上面的属性。有关如何通过JavaScript设置新值的示例
conn.publish('set_MyProp',{value:'NewValue'});
有关更多信息,请参见客户端API。
但是客户端API的页面上没有任何信息
我很难确定JavaScript代码的等效C#客户端代码 conn.publish('set_MyProp',{value:'NewValue'});
非常感谢您的帮助。
好吧,通过尝试一个错误,我发现了一个困难的办法:
Client.Send(new { value = "NewValue" }, "set_MyProp");
等效于以下代码:
conn.publish('set_MyProp',{value:'NewValue'});
注意“值”的情况!!!不要大写这个词!
更新
我创建了两个扩展方法,这些方法使获取和设置属性值变得非常容易(还有一个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。
困扰我的差异是:
publish
在JavaScript与Send
C#中value, event
在C#中,event, value
在JavaScript中本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句