Code Monkey home page Code Monkey logo

blazorwebsockethelper's Introduction

Blazor WebSocket Helper

If you like my blazor works and want to see more open sourced repos please support me with paypal donation

Please send email if you consider to hire me.

!!!!!!!!!!!! Readme should be updated because it is relevant to version 1.0.1 !!!!!!!!!!!!

This repo contains Pure WebSocket library for blazor.

Helper is available on nuget

For install use command - Install-Package BlazorWebSocketHelper

After installing package please add bellow script to your index.html

<script src="_content/BlazorWebSocketHelper/BwsJsInterop.js"></script>

You can use websocket in blazor easy and convenient way using this library.

image

Usage sample:

markup:

<div style="margin:5px">
    <button class="btn btn-primary" style="width:120px;margin:5px" onclick="@WsConnect">@Ws_Button</button>
    <span style="margin:5px">URL</span>
    <input bind="@Ws_URL" style="margin:5px;width:250px" />
    <span style="width:120px;margin:5px">status:@Ws_Status</span>
    <button class="btn btn-primary" style="margin:5px" onclick="@WsGetStatus">Get status</button>
</div>
<br />
<div style="margin:5px">
    <input style="margin:5px;width:250px" bind="@Ws_Message" />
    <button class="btn btn-primary" style="margin:5px" onclick="@WsSendMessage">Send</button>
</div>
<br />
<span style="margin:5px">Log:</span>
<br />
@foreach (var item in WebSocketHelper1.Log)
{
    <br />
    <CompMessage bwsMessage="@item" parent="@this" />
    <br />

}

code:

  public string Ws_URL = "wss://echo.websocket.org";
  protected WebSocketHelper WebSocketHelper1;
  
  public void WsConnect()
  {
    WebSocketHelper1 = new WebSocketHelper(Ws_URL);

    WebSocketHelper1.OnStateChange = WsOnStateChange;
    WebSocketHelper1.OnMessage = WsOnMessage;
    WebSocketHelper1.OnError = WsOnError;
  }
  public void WsOnStateChange(short par_state)
  {
    Ws_Status = WebSocketHelper1.bwsState.ToString();
    StateHasChanged();
  }
  public void WsOnError(string par_error)
  {
    BwsJsInterop.Alert(par_error);
  }
  public void WsOnMessage(string par_message)
  {
    StateHasChanged();
  }
  public void WsSendMessage()
  {
      if (WebSocketHelper1.bwsState == BwsEnums.BwsState.Open)
      {
          if (!string.IsNullOrEmpty(Ws_Message))
          {
              WebSocketHelper1.send(Ws_Message);
              Ws_Message = string.Empty;
              StateHasChanged();
          }
          else
          {
              BwsJsInterop.Alert("Please input message");
          }
      }
      else
      {
          BwsJsInterop.Alert("Connection is closed");
      }
  }
  

For example this helper was used here

You can see code here

Any PRs are welcome.

blazorwebsockethelper's People

Contributors

lupusa87 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.