av2020
av2020

Reputation: 13

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.

Upvotes: 1

Views: 666

Answers (1)

Athanasios Kataras
Athanasios Kataras

Reputation: 26372

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.

Upvotes: 1

Related Questions