如何使用WampSharp处理错误和连接关闭

Gau*_*ang 4 c# publish-subscribe websocket autobahn wampsharp

我一直在使用WampSharp,即提供的客户端库与autobahn wamp websocket连接.

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

DefaultWampChannelFactory channelFactory = new DefaultWampChannelFactory();
channel = channelFactory.CreateChannel(serverAddress);
channel.Open();
Run Code Online (Sandbox Code Playgroud)

这里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));
}
Run Code Online (Sandbox Code Playgroud)

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

webSocket.Error += new EventHandler<SuperSocket.ClientEngine.ErrorEventArgs>(webSocket_Error);
webSocket.Closed += new EventHandler(webSocket_Closed); 
Run Code Online (Sandbox Code Playgroud)

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

提前致谢.

dar*_*rkl 6

试试这个:

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();
Run Code Online (Sandbox Code Playgroud)