简体   繁体   中英

C# websocket atmosphere client example

Is it possible to use the atmosphere in C # Windows Form? The server side uses the Java language. I want to connect to WebSocket via C # and view incoming messages.

The answer is both yes and no. Atmosphere is a framework that supports web sockets, sender sent events and long polling.

In the above sense, the answer is no. There is no out of the box library that handles all that for you.

On the other hand, since you can support web sockets from your application, I think it's just a matter of connecting to the atmosphere server by using web sockets.

One solution for this, could be this library here: https://github.com/Marfusios/websocket-client

Example:

var exitEvent = new ManualResetEvent(false);
var url = new Uri("wss://xxx");

using (var client = new WebsocketClient(url))
{
    client.ReconnectTimeout = TimeSpan.FromSeconds(30);
    client.ReconnectionHappened.Subscribe(info =>
        Log.Information($"Reconnection happened, type: {info.Type}"));

    client.MessageReceived.Subscribe(msg => Log.Information($"Message received: {msg}"));
    client.Start();

    Task.Run(() => client.Send("{ message }"));

    exitEvent.WaitOne();
}

I have not tried it in practice, but I don't see why it wouldn't work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM