【问题标题】:How to handle Errors and Connection Close using WampSharp如何使用 WampSharp 处理错误和连接关闭
【发布时间】:2014-09-14 15:44:48
【问题描述】:

我一直在使用 WampSharp,即提供用于连接高速公路 wamp websocket 的客户端库。

我已经使用以下代码(使用 WampSharp)使用 .Net 客户端应用程序成功连接了我在 python 中创建的 Autobahn Wamp Websocket:

DefaultWampChannelFactory channelFactory = new DefaultWampChannelFactory();
channel = channelFactory.CreateChannel(serverAddress);
channel.Open();

这里的 serverAddress 是:127.0.0.1:8000(即我的 websocket 从我本地机器的 8000 端口号开始)。

我正在使用 pubsub 机制来交换 autobahn wamp websocket 提供的数据,使用以下代码:

public void Subscribe()
{
    ISubject<string> subscribe1 = channel.GetSubject<string>(@"simple/topicSubject1");
    IDisposable subject1 = subscribe1.Subscribe(msg => MessageRecieved(msg));
}

public void Publish()
{
    ISubject<string> subjectForPublish = channel.GetSubject<string>(@"simple/topicSubject1");
    subjectForPublish.OnNext(sd.SerializeObject(DataToPublish));
}

这些所有过程都已成功完成。 我面临的问题是我找不到任何处理程序来处理错误和连接丢失,就像我们在传统 websocket 中所做的那样。 在传统的 websocket 中,我们有如下处理程序:

webSocket.Error += new EventHandler<SuperSocket.ClientEngine.ErrorEventArgs>(webSocket_Error);
webSocket.Closed += new EventHandler(webSocket_Closed); 

我需要使用 wampsharp 来实现上述功能。

提前致谢。

【问题讨论】:

    标签: c# websocket publish-subscribe autobahn wampsharp


    【解决方案1】:

    试试这个:

    DefaultWampChannelFactory factory = new DefaultWampChannelFactory();
    IWampChannel<JToken> channel = factory.CreateChannel("ws://localhost:9090/ws");
    
    IWampClientConnectionMonitor monitor = channel.GetMonitor();
    monitor.ConnectionError += ConnectionError;
    monitor.ConnectionEstablished += ConnectionEstablished;
    monitor.ConnectionLost += ConnectionLost;
    
    await channel.OpenAsync();
    

    【讨论】:

    • 嗨。非常感谢您的回复。我会试一试的。
    猜你喜欢
    • 2020-05-24
    • 2020-02-07
    • 2011-09-27
    • 2012-05-01
    • 2020-02-21
    • 2021-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多