GithubHelp home page GithubHelp logo

andrewdavey / signalr Goto Github PK

View Code? Open in Web Editor NEW

This project forked from signalr/signalr

2.0 2.0 1.0 970 KB

Async signaling library for ASP.NET to help build real-time, multi-user interactive web applications.

Home Page: http://signalr.net

License: MIT License

signalr's Introduction

LICENSE

MIT License http://www.opensource.org/licenses/mit-license.php

Get it on NuGet!

SignalR is broken up into a few package on NuGet:

  • SignalR - A meta package that brings in SignalR.Server and SignalR.Js (you should install this)

  • SignalR.Server - Server side components needed to build SignalR endpoints

  • SignalR.Js - Javascript client for SignalR

  • SignalR.Client - .NET client for SignalR

  • SignalR.Ninject - Ninject dependeny resolver for SignalR

To get started:

Install-Package SignalR

RAW Connection API

Server

Create a class the derives from PersistentConnection:

using SignalR;

public class MyConnection : PersistentConnection {
    protected override Task OnReceivedAsync(string clientId, string data) {
        // Broadcast data to all clients
        return Connection.Broadcast(data);
    }
}

Setup Routing

Make a route for your connection:

Global.asax

using System;
using System.Web.Routing;
using SignalR.Routing;

public class Global : System.Web.HttpApplication {
    protected void Application_Start(object sender, EventArgs e) {
        // Register the route for chat
        RouteTable.Routes.MapConnection<MyConnection>("echo", "echo/{*operation}");
    }
}

Client

Javascript

Add these scripts to your page:

<script src="Scripts/jquery-1.6.2.min.js" type="text/javascript"></script>
<script src="Scripts/jquery.signalR.min.js" type="text/javascript"></script>

HTML:

<script type="text/javascript">
$(function () {
    var connection = $.connection('echo');

    connection.received(function (data) {
        $('#messages').append('<li>' + data + '</li>');
    });
    
    connection.start();
    
    $("#broadcast").click(function () {
        connection.send($('#msg').val());
    });
});
</script>

<input type="text" id="msg" />
<input type="button" id="broadcast" />

<ul id="messages">
</ul>

C# (Events)

var connection = new Connection("http://localhost/echo");
connection.Received += data => {
    Console.WriteLine(data);
};

connection.Start().Wait();
connection.Send("From C#");

C# (IObservable)

var connection = new Connection("http://localhost/echo");
connection.AsObservable()
          .Subscribe(Console.WriteLine);

connection.Start().Wait();
connection.Send("From C#");

Higher level API

Server

public class Chat : Hub {
    public void Send(string message) {
        // Call the addMessage method on all clients
        Clients.addMessage(message);
    }
}

Client

Javascript

Import the magic script to generate the server side proxy

<script src="Scripts/jquery-1.6.2.min.js" type="text/javascript"></script>
<script src="Scripts/jquery.signalR.min.js" type="text/javascript"></script>
<script src="/signalr/hubs" type="text/javascript"></script>

HTML:

<script type="text/javascript">
$(function () {
    // Proxy created on the fly
    var chat = $.connection.chat;
    
    // Declare a function on the chat hub so the server can invoke it
    chat.addMessage = function(message) {
        $('#messages').append('<li>' + message + '</li>');
    };
    
    $("#broadcast").click(function () {
        // Call the chat method on the server
        chat.send($('#msg').val())
            .fail(function(e) { alert(e); }) // Supports jQuery deferred
    });
    
    // Start the connection
    $.connection.hub.start();
});
</script>

<input type="text" id="msg" />
<input type="button" id="broadcast" />

<ul id="messages">
</ul>

signalr's People

Contributors

davidfowl avatar damianedwards avatar ayende avatar ducas avatar jonathanptew avatar

Stargazers

Schalk avatar Andrew Davey avatar

Watchers

Andrew Davey avatar James Cloos avatar

Forkers

juliofederal

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.