从OnOpen事件发送时未收到消息 - c#

我正在使用XSockets 3.x(最新)。

我已经设置了一个控制器:

public class NotificationsController : XSocketController
{
    public NotificationsController()
    {
        // Bind an event for once the connection has been opened.
        this.OnOpen += OnConnectionOpened;

        // Bind an event for once the connection has been closed.
        this.OnClose += OnConnectionClosed;
    }

    void OnConnectionOpened(object sender, XSockets.Core.Common.Socket.Event.Arguments.OnClientConnectArgs e)
    {
        // Notify everyone of the new client.
        this.SendToAll(new TextArgs("New client. Called right from OnConnectionOpened.", "notify"));
    }
}

可以看出,它只是一个基本的控制器,它监听连接并在建立新连接后通知每个人,但是,它不起作用-消息未收到。 Chrome的开发工具也没有显示Frame。

我的网络客户端:

var xs = new XSockets.WebSocket("ws://" + window.location.hostname + ":1338/notifications");

xs.onopen = function(e)
{
    console.log("Connected", e);
};

xs.on('notify', function(data)
{
    console.log(data);
});

在控制台中显示以下输出:

这在网络选项卡->框架中:

我可以通过将SendToAll调用推迟到System.Threading.Timer并超时来解决此问题。我的调试显示,50ms不一致,因此我将其设置为300ms,这似乎还可以,但是计时器感觉很麻烦。

我该如何解决该问题?
当XSockets真正为客户端准备好了时,也许有一个我可以听的事件吗?

参考方案

在3.0.6中这样做的原因是所有与发布和订阅有关。

这意味着消息将仅在服务器上订阅了该主题的情况下发送给客户端。在您提供的示例中,您似乎只有一个客户。自绑定以来,此客户端将不会收到自己的“通知”消息

xs.on("notify",callback);

发生OnOpen时未绑定在服务器上...因此,客户端连接将无法获取有关其自身连接的信息。

有几种解决方法...

1在绑定通知之前,不要通知连接。这是通过在绑定中添加第三个回调来完成的。当订阅绑定在服务器上时,将触发该回调。像这样

xs.on('notify', function(d){console.log('a connection was established')}, function(){console.log('the server has confirmed the notify subscription')});

您将在第一个回调内调用服务器方法来通知其他人...

2发送信息之前,请在服务器上进行绑定,这可能是一个更麻烦的选择。

void OnConnectionOpened(object sender, OnClientConnectArgs e)
{
    //Add the subscription
    this.Subscribe(new XSubscriptions{Event = "notify",Alias = this.Alias});
    // Notify everyone of the new client.
    this.SendToAll("New client. Called right from OnConnectionOpened.", "notify");
}

3使用XSockets.NET 4.0 BETA,它具有改进的通信功能并允许RPC或Pub / Sub。在4.0中,您会在OnOpen事件中这样做

//Send the message to all clients regardless of subscriptions or not...
this.InvokeToAll("New client. Called right from OnConnectionOpened.", "notify");

//Client side JavaScript
xs.controller('NotificationsController').on('notify', function(data){
    console.log(data);
});

//Client side C#
xs.Controller("NotificationsController").On<string>('notify', s => Console.WriteLine(s));

4.0还有很多其他重要的改进...您可以在这里阅读到您对4.0感兴趣的内容http://xsockets.github.io/XSockets.NET-4.0/

//可能是样本中的错别字...从我的头顶开始写...

如何在没有for循环的情况下在Javascript中使用Django模板标签 - javascript

我想在JavaScript中使用模板变量:我的问题是在javascript代码中使用for循环,for循环之间的所有事情都会重复..但我不想要....下面粘贴了我的代码..有人可以告诉我更好的方法吗这..因为这看起来很丑..这是我的代码: {% block extra_javascript %} <script src="/static/js…

Mongo抛出“元素名称'名称'无效”异常 - c#

我正在更新一个简单的字段。var filterDocument = new BsonDocument { { "name", "alice" } }; var newDocument = new BsonDocument { { "name", "Alice" } }; coll…

T-SQL等价的正则表达式'\ b' - c#

我正在将利用regex的CLR函数转换为SQL函数。我知道SQL Server并不完全支持正则表达式,但是我只需要一种情况就可以搜索单词。搜索字段值:{"Id":1234, "Title": "The quick brown"}.NET中的正则表达式模式:'\b' + '…

.NET C#Webbrowser填充输入,不带ID或类名 - javascript

我需要在网络浏览器中填写一个输入,但这不起作用。我认为必须使用name属性来完成,但是怎么做呢?foreach (HtmlElement login in webBrowser1.Document.GetElementsByTagName("input")) { if (login.GetAttribute("name"…

当回复有时是一个对象有时是一个数组时,如何在使用改造时解析JSON回复? - java

我正在使用Retrofit来获取JSON答复。这是我实施的一部分-@GET("/api/report/list") Observable<Bills> listBill(@Query("employee_id") String employeeID); 而条例草案类是-public static class…