Giter Site home page Giter Site logo

coder-qing / centrifugo-dotnet Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mt89vein/centrifugo-dotnet

0.0 0.0 0.0 701 KB

C# client for centrifugo

License: MIT License

Shell 0.05% JavaScript 0.34% C# 88.50% CSS 2.02% HTML 7.33% Batchfile 0.62% Dockerfile 1.15%

centrifugo-dotnet's Introduction

Centrifuge client for dotnet

This client can connect to Centrifugo using websockets.

Work in progress now.

Look at sample project for details.

Playground

Clone, start docker-compose in root folder.

on first window open home page, on the other hit /new?text=my-text

Usage api surface

var ws = new WebsocketClient(new Uri("ws://localhost:8000/connection/websocket?format=protobuf"));

// create client
var centrifugo = new CentrifugoClient(ws);

// setup event handlers
centrifugo.OnConnect(e => logger.LogInformation(e.Client.ToString()));
centrifugo.OnError(e => logger.LogInformation(e.ToString()));
// OnRefresh, OnDisconnect not implemented yet.

// create new subscription
var subscription = centrifugo.CreateNewSubscription("test-channel");

// setup for event handlers
// both sync and async supported.
subscription.OnSubscribe(e => logger.LogInformation("subscribed"));
subscription.OnUnsubscribe(e => logger.LogInformation("unsubscribed"));
subscription.OnPublish(e => logger.LogInformation(e.ToString()));
// also OnJoin, OnLeave etc events

// generate JWT token with correct secret key.
// It must be identical with 'token_hmac_secret_key' in centrifugo configuration.
var token = await tokenProvider
        .GenerateTokenAsync("some_client_identifier", "Hi, my name is Jack");

// set jwt-token
centrifugo.SetToken(token);

// open ws connection and connection handshake to centrifugo.
await centrifugo.ConnectAsync();

// send subscribe message with configured above subscription.
await centrifugo.SubscribeAsync(subscription);

// prepare message to publish
var msg = new TestMessage
{
    Msg = text ?? "Hello",
    Id = Guid.NewGuid()
};

// convert it to json, and then get bytes
var json = JsonConvert.SerializeObject(msg);
var payload = Encoding.UTF8.GetBytes(json);

// publish to channel
await centrifugo.PublishAsync(subscription.Channel, payload);

// hint: under the hood used Google protocol buffers, but 
// you can pass any payload.

Contribute

Feel free for creation issues, or PR :)

License

Copyright © 2020 Shamil Sultanov

The MIT licence.

centrifugo-dotnet's People

Contributors

mt89vein avatar dependabot[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.