GithubHelp home page GithubHelp logo

mlucas99 / owin.websocket Goto Github PK

View Code? Open in Web Editor NEW

This project forked from bryceg/owin.websocket

0.0 1.0 0.0 609 KB

A library for handling OWIN WebSocket connections

License: MIT License

C# 100.00%

owin.websocket's Introduction

Owin.WebSocket

An library for handling OWIN WebSocket connections

Build status

This library was born from a need to replace the SignalR default of json serialization. Some code is borrowed from SignalR and built upon to handle the web socket connection, and allow the need to handle the serialization seperately. By default most people will use JSON due to the wide understanding of it and native support in the browsers. Other types could easily be swapped in such as Protobuf or msgpack as you see fit.

Getting Started:

Install the Nuget package: https://www.nuget.org/packages/Owin.WebSocket/

1) Inherit from WebSocketConnection

using Owin.WebSocket;

public class MyWebSocket : WebSocketConnection
{
    public override void OnMessageReceived(ArraySegment<byte> message, WebSocketMessageType type)
    {
       //Handle the message from the client
       
       //Example of JSON serialization with the client
       //var json = Encoding.UT8.GetString(message.Array, message.Offset, message.Count);
       //Use something like Json.Net to read the json
    }
    
    //public override void OnOpen(){}
    //public override void OnClose(WebSocketCloseStatus closeStatus, string closeDescription){}
    //public override bool Authenticate(IOwinContext requestContext){return true;}
}

2) Map the route for the web socket connection
In your OWIN Startup, set the URI mapping for the websocket connection. A new instance of this class will be instantiated per web socket connection. Refer To dependency injection at the bottom of this file if you wish to change the default resolver and use your own IoC.

using Owin.WebSocket.Extensions;

//For static routes http://foo.com/ws use MapWebSocketRoute and attribute the WebSocketConnection with [WebSocketRoute('/ws')]
app.MapWebSocketRoute<MyWebSocket>();

//For static routes http://foo.com/ws use MapWebSocketRoute
app.MapWebSocketRoute<MyWebSocket>("/ws");

//For dynamic routes where you may want to capture the URI arguments use a Regex route
app.MapWebSocketPattern<MyWebSocket>("/captures/(?<capture1>.+)/(?<capture2>.+)");

3) Sending data to the client
To send something to the client the WebSocketConnection exposes 3 methods.

.SendText() for sending text to the client using the text message type flag.
.SendBinary() for sending binary to the client using the binary messge type flag.
.Send() allows you specify the message type for the data you are sending

using Owin.WebSocket;

public class MyWebSocket : WebSocketConnection
{
    public override void OnMessageReceived(ArraySegment<byte> message, WebSocketMessageType type)
    {
       //Handle the message from the client
       
       //Example of JSON serialization with the client
       var json = Encoding.UTF8.GetString(message.Array, message.Offset, message.Count);

        var toSend = Encoding.UTF8.GetBytes(json);
        
        //Echo the message back to the client as text
        SendText(toSend, true);
    }
}
Javascript Client:
http://msdn.microsoft.com/en-us/library/ie/hh673567(v=vs.85).aspx

#####Dependency Injection for WebSocketConnection instance:

The Microsoft Common Service Locator pattern is used for dependency injection. To use your own IoC pass it along to the MapWebSocketRoute call

//Autofac example
app.MapWebSocketRoute<MyWebSocket>("/ws", new AutofacServiceLocator(container));

owin.websocket's People

Contributors

bryceg avatar cata avatar

Watchers

 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.