无法使用c#订阅WebSocket上的特定频道
Unable to subscribe to a specific channel on WebSocket using c#
我在 JavaScript 中有这种方式的 websocket 监听器。
var connectionLostMessage = null;
function websocketConnect() {
var conn = new ab.Session('ws://abc.xyz.dd:8080?time=1627624930.3964',
function () {
clearTimeout(connectionLostMessage);
connectionLostMessage = null;
conn.subscribe('316627', function (topic, data) {
//some code to use data display
});
},
function () {
//When connection fails
//Connection fail handling
setTimeout(function () {
websocketConnect();
}, 1000)
},
{'skipSubprotocolCheck': true}
);
}
websocketConnect();
我想使用 c# 连接并订阅频道,如下行 JavaScript 中所示。
conn.subscribe('316627', function (topic, data) {
我试过这种方式,但无法通过上述方式订阅。
public async Task WebsocketClientTest()
{
var url = new Uri("ws://abc.xyz.dd:8080");
var exitEvent = new ManualResetEvent(false);
using (var client = new WebsocketClient(url))
{
client.MessageReceived
.Subscribe(msg => Console.WriteLine($"msg.Text: {msg.Text}, msg.MessageType: {msg.MessageType}"));
await client.Start();
exitEvent.WaitOne();
}
Console.ReadLine();
}
我需要在 .Net Core 中编写一个控制台应用程序。请建议与 .Net Core 3.1
兼容的任何帮助或第三方
非常感谢!
我按照上述评论中 jdweng 的建议找到了解决方案。我唯一做错了要发送的消息的格式。我的问题在问题编辑后也缺少发送行。
client.Send($"[5, \"123456"]");
我在 JavaScript 中有这种方式的 websocket 监听器。
var connectionLostMessage = null;
function websocketConnect() {
var conn = new ab.Session('ws://abc.xyz.dd:8080?time=1627624930.3964',
function () {
clearTimeout(connectionLostMessage);
connectionLostMessage = null;
conn.subscribe('316627', function (topic, data) {
//some code to use data display
});
},
function () {
//When connection fails
//Connection fail handling
setTimeout(function () {
websocketConnect();
}, 1000)
},
{'skipSubprotocolCheck': true}
);
}
websocketConnect();
我想使用 c# 连接并订阅频道,如下行 JavaScript 中所示。
conn.subscribe('316627', function (topic, data) {
我试过这种方式,但无法通过上述方式订阅。
public async Task WebsocketClientTest()
{
var url = new Uri("ws://abc.xyz.dd:8080");
var exitEvent = new ManualResetEvent(false);
using (var client = new WebsocketClient(url))
{
client.MessageReceived
.Subscribe(msg => Console.WriteLine($"msg.Text: {msg.Text}, msg.MessageType: {msg.MessageType}"));
await client.Start();
exitEvent.WaitOne();
}
Console.ReadLine();
}
我需要在 .Net Core 中编写一个控制台应用程序。请建议与 .Net Core 3.1
兼容的任何帮助或第三方非常感谢!
我按照上述评论中 jdweng 的建议找到了解决方案。我唯一做错了要发送的消息的格式。我的问题在问题编辑后也缺少发送行。
client.Send($"[5, \"123456"]");