Code Monkey home page Code Monkey logo

websocketproxy's Introduction

WebSocketProxy build status

WebSocketProxy is a lightweight C# library which allows you to connect to an HTTP server and websocket server through the same TCP port. This library is independent on which web frameworks are used for handling websocket and http requests.

Installation

Installation can be done using NuGet.

Install-Package WebSocketProxy

Sample Usage

The project "WebSocket.Sample" contains a sample usage using NancyFX and Fleck.

The first step is to build the configuration object in which you set the endpoints that are listening for HTTP (Nancy) and WebSocket requests (Fleck) and the public endpoint which will be listening to both kinds of requests.

TcpProxyConfiguration configuration = new TcpProxyConfiguration()
            {
                PublicHost = new Host()
                {
                    IpAddress = IPAddress.Parse("0.0.0.0"),
                    Port = 8080
                },
                HttpHost = new Host()
                {
                    IpAddress = IPAddress.Loopback,
                    Port = 8081
                },
                WebSocketHost = new Host()
                {

                    IpAddress = IPAddress.Loopback,
                    Port = 8082
                }
            };

Then, initialize Nancy and Fleck, followed by the WebSocketServer.

            using (var nancyHost = new NancyHost(new Uri("http://localhost:8081")))
            using (var websocketServer = new WebSocketServer("ws://0.0.0.0:8082"))
            using (var tcpProxy = new TcpProxyServer(configuration))
            {
                // Initialize Nancy
                nancyHost.Start();
                
                // Initialize Fleck
                websocketServer.Start(connection =>
                {
                    connection.OnOpen = () => Console.WriteLine("Connection on open");
                    connection.OnClose = () => Console.WriteLine("Connection on close");
                    connection.OnMessage = message => Console.WriteLine("Message: " + message);
                });

                // Initialize the proxy
                tcpProxy.Start();

                Console.WriteLine("Press [Enter] to stop");
                Console.ReadLine();
            }

By pointing the web browser to the port 8080, you will receive an html page which initializes a websocket connection.

SSL Support

This library supports HTTPS and WSS. Simply pass the certificate path and password in the configuration object and all incoming requests will be unencrypted and routed to the corresponding library.

websocketproxy's People

Contributors

tiagooliveiramarques avatar meirionhughes avatar abelsilva avatar

Watchers

James Cloos avatar  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.